Detect red pixel - translate OpenCV to EmguCV

Ask questions on using Emgu CV with C++
Post Reply
manguscik
Posts: 4
Joined: Thu Mar 05, 2009 3:41 am

Detect red pixel - translate OpenCV to EmguCV

Post by manguscik » Sun Apr 19, 2009 5:42 pm

Hi guys.
I`m trying to translate the code from OpenCV to EmguCV, but I do not go.
I want to detect red pixel in the image.
Please help.

Code: Select all

					uchar* pixel_src;
					uchar* pixel_dest;
					height = frameCV->height;
					width  = frameCV->width;
                	
						 for (i=0;i<height;i++){
							for(j=0;j<width;j++){
							pixel_src= &CV_IMAGE_ELEM(frameCV ,uchar,i,j*3);
							pixel_dest= &CV_IMAGE_ELEM(frame_red,uchar,i,j*3);
							
			
							double blue_point=(double)pixel_src[0];
							double green_point=(double)pixel_src[1];
							double red_point=(double)pixel_src[2];
						
							//the ratio of red to green
							double tmp1=red_point/(green_point+0.001);
							//the ratio of red to blue
							double tmp2=red_point/(blue_point+0.001);
							
							if ((tmp1>parameter1)&&(tmp2>parameter2)&&(red_point>parameter3)) {
							   //detect red color 
							   pixel_dest[0]=pixel_src[0];
							   pixel_dest[1]=pixel_src[1];
							   pixel_dest[2]=pixel_src[2];
							}
							else{
							  //color black
							  pixel_dest[0]=0;
							  pixel_dest[1]=0;
							  pixel_dest[2]=0;
							}
						}


canming
Site Admin
Posts: 1038
Joined: Thu Feb 28, 2008 4:16 pm

Re: Detect red pixel - translate OpenCV to EmguCV

Post by canming » Mon Apr 20, 2009 9:59 pm

If frameCV is Image<Bgr, Byte>^, to get red pixels, I will do:

Code: Select all

Image<Bgr, Byte>^ frameRed = frameCV->Copy( frameCV->Convert<Hsv,Byte>()[0]->InRange(new Gray(10), new Gray(150))->Not() );
Change [10, 150] to [20, 140] depends on how you think red is.

manguscik
Posts: 4
Joined: Thu Mar 05, 2009 3:41 am

Re: Detect red pixel - translate OpenCV to EmguCV

Post by manguscik » Wed Apr 22, 2009 5:53 pm

Thank canming.

Your code is a good idea, but contains little error. Below is a corrected version.

Code: Select all

Image<Bgr, Byte>^ frameRed = frame2->Copy(frame2->Convert<Hsv,Byte>()[0]->InRange(Gray(10),Gray(150))->Not());
I have a question.
Is there a possibility to save the following code in one line:

Code: Select all

//detect red color
Image<Bgr, Byte>^ frameRed = frame2->Copy(frame2->Convert<Hsv,Byte>()[0]->InRange(Gray(10),Gray(150))->Not());
//detect the intensity of the red color
frameRed = frameRed->Copy( ( frameRed->Convert<Hsv,Byte>()[1]->InRange(Gray(0),Gray(80)) )->Not() );
The compiler forest of code, but the program does not work.

Code: Select all

Image<Bgr, Byte>^ frameRed = frame2->Copy(frame2->Convert<Hsv,Byte>() [0]->InRange(Gray(10),Gray(150))[1]->InRange(Gray(0),Gray(80)) ->Not() );

Post Reply