accés aux pixels d''une image en couleur et les mettre dans un tableau

This is a discussion on accés aux pixels d''une image en couleur et les mettre dans un tableau within the C Programming forums, part of the General Programming Boards category; je programme en C et j'utilise OpenCv j'essaye de stocker la valeur des pixels dans une matrice Code: uchar mat[307200][3]; ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    8

    accés aux pixels d''une image en couleur et les mettre dans un tableau

    je programme en C et j'utilise OpenCv
    j'essaye de stocker la valeur des pixels dans une matrice
    Code:
    uchar mat[307200][3];
    data      = (uchar *)image_ref->imageData;
           for(i=0;i<height;i++) 
    	   {
    		   for(j=0;j<width;j++) 
    		   {
    			   for(k=0;k<channels;k++)
    			   {
    		           mat[i][j]=data[i*step+j*channels+k] ;
    				   
    			   }
    			   printf("%d\t",mat[i][j] );
    		   }
    	   }
    mais le pb c'est que j'obtient pas la meme valeur que lorsque je fais
    Code:
    CvScalar s=cvGet2D(image_ref,i,j);
    svp ou est le probleme??
    mercii

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    The written language on this board is English.
    Can you edit your post to English please.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    The written language on this board is English.
    Can you edit your post to English please.
    I don't see the answer to her problem... but she says she's trying to import an image matrix into an array and having problems converting it to a 2d array.

    And yes she needs to post in English from here on... I'm no translator.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    Hey, french!! God, although i knew it very well in the past, know i've completely forgotten it!
    Devoted my life to programming...

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sipher View Post
    Hey, french!! God, although i knew it very well in the past, know i've completely forgotten it!
    I haven't read, spoken or heard French since high school (Actually, most Canadians do not speak french, it's only required for government jobs)...

    I'm amazed I took any meaning from it whatsoever...

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    i'm sorry for my french post and i rectifie that i said in english, sorry

    I programming in C and I use OpenCV
    I try to store the value of the pixels in a matrix
    Code:
    uchar mat[480][640];
    uchar *data  = (uchar *)image->imageData;
           for(i=0;i<height;i++) 
    	   {
    		   for(j=0;j<width;j++) 
    		   {
    			   for(k=0;k<channels;k++)
    			   {
    		           mat[i][j]=data[i*step+j*channels+k] ;
    				   
    			   }
    			   
    		   }
                      printf("n\");
    	   }
    I want to know if is it just or no and how can I transforme my color picture to a gray level ?
    thinks

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Presumably, your channels are say 0,1,2 to represent the RGB components?

    Quote Originally Posted by some site found by google
    Convert RGB image or colormap to grayscale - MATLAB

    Algorithm

    rgb2gray converts RGB values to grayscale values by forming a weighted sum of the R, G, and B components:

    0.2989 * R + 0.5870 * G + 0.1140 * B
    - Get your 3 values for RGB
    - Normalise the values in the range [0..1), by dividing by 256
    - Apply the above function to get a brightness in the range [0..1)
    - Scale back to whatever resolution of bits you have for your grayscale.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    yes Salem my channels are represented by 0,1 et 2
    that's what i do
    Code:
       // convert to gray
    
           for(i=0;i<height;i++) 
    	   {
    		   for(j=0;j<width;j++) 
    		   {
    			   
    			           mat[i][j]= (data[i*step+j*channels]*0.114 
    						          +data[i*step+j*channels+1]*0.587
    								  +data[i*step+j*channels+2]*0.2989)/256 ;
         
    		   }
    		 
    	   }
    
            // apply the matrix for my picture
    
    	   for(i=0;i<height;i++) 
    	   {
    		   for(j=0;j<width;j++) 
    		   {
    				data[i*step+j]=mat[i][j] ;
    		   }
    	   }
    please tell me that is just else give some clues , i need the help
    thanks

  9. #9
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Maybe?
    Code:
    mat[i][j]= ((data[i * step + j * channels] / 256) * 0.114 +
                (data[i * step + j * channels + 1] / 256) * 0.587 +
                (data[i * step + j * channels + 2] /256) * 0.2989);
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Use a debugger, or add some print statements, to observe what is going on.
    Are the RGB values you're pulling out of your data array correct?
    What about your value of step - are you stepping correctly through the array?

    Or create a really small "image" consisting of say 10 RGB values, and feed that into your conversion function to see what values emerge.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    thanks for all

    I managed to convert my image to gray level without dividing by 256 because when I divide I obtain an image completely white !!!!!

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    For future reference, the white image is probably because you were doing integer division: Question 3.15.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    8

    sobel filter

    it's me again )

    I managed to convert my image to gray level and I applied the Sobel filter for edge detection and I haven't got good result .I send you the original_picture , sobel_picture and the code of my fonction and try to help me please
    thanks

    Code:
    	int ConvY[3][3]={1,2,1,0,0,0,-1,-2,-1};
    	int ConvX[3][3]={1,0,-1,2,0,-2,1,0,-1};
    	double mX,mY,x,y;
    	int i,j,k,l;
    	double moy,g;
    
    
    	for(i=(pImage->height)-2;i>=1;i--)
    		for(j=(pImage->width)-2;j>=1;j--)
    		{
    			mX=0;
    			mY=0;
    			for( k=-1;k<=1;k++)
    			{
    				for(l=-1;l<=1;l++)
    					
    				{
    					mX+=(double)pImage->imageData[(i+k)*pImage->widthStep+(j+l)*pImage->nChannels]*ConvX[k+1][l+1];
    					mY+=(double)pImage->imageData[(i+k)*pImage->widthStep+(j+l)*pImage->nChannels]*ConvY[k+1][l+1];	
    				}
    				moy=sqrt((double)mX*mX+mY*mY);
    				if(moy>255) {moy=255;}
    
    				pImage->imageData[i*pImage->widthStep+j*pImage->nChannels]=moy;
    				pImage->imageData[i*pImage->widthStep+j*pImage->nChannels+2]=pImage->imageData[i*pImage->widthStep+j*pImage->nChannels];
    				pImage->imageData[i*pImage->widthStep+j*pImage->nChannels+1]=pImage->imageData[i*pImage->widthStep+j*pImage->nChannels];
    		
    			}
    		}
    }
    this is the original_picture Image : original_picture.jpg

    and the sobel_picture Image : sobel_picture.jpg

    thanks

  14. #14
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    If you're trying to convert RGB values to a single GrayScale value, you've got this all wrong!
    This's how i do it, and it works pretty fine:

    Example:
    Code:
    GrayScale = (R + G + B)/3
    Devoted my life to programming...

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    I don't suppose you bothered to do any research on this subject?
    It might be a "gray" answer, but it won't be the equivalent grayscale image.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Big Problem With Passing an Matrix to Main Function
    By Maragato in forum C Programming
    Replies: 4
    Last Post: 06-14-2004, 11:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21