how can i access to element of an image and modify it

Ask questions on using Emgu CV with C++
Post Reply
iman_kh
Posts: 11
Joined: Tue Sep 09, 2008 1:46 am

how can i access to element of an image and modify it

Post by iman_kh » Sun Sep 14, 2008 9:49 pm

hi
i want to modify cells of image
how can i access to element of an image and modify it
so thanks

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

Re: how can i access to element of an image and modify it

Post by canming » Tue Sep 16, 2008 8:22 am

It's Simple. If you have an image

Code: Select all

Image<Bgr^, Byte>^ img;
You can obtain the value on the (x, y) position by calling

Code: Select all

Bgr^ color = img[y,x];
To set the color, call

Code: Select all

img[y,x] = color

Scissorhands
Posts: 3
Joined: Fri Sep 26, 2008 4:14 am

Re: how can i access to element of an image and modify it

Post by Scissorhands » Fri Sep 26, 2008 4:25 am

Hi, I have the same problem!
I have to tranlsate this C++ OpenCv code

for( int i = 1; i < mask->width-1; i++ )
for( int j = 1; j < mask->height-1; j++ )
{
uchar idx = CV_IMAGE_ELEM( mask, uchar, j, i );
if(idx==255){
uchar blue = CV_IMAGE_ELEM( color_img0, uchar, j-1, (i-1)*3);
uchar green = CV_IMAGE_ELEM( color_img0, uchar, j-1, (i-1)*3 +1);
uchar red = CV_IMAGE_ELEM( color_img0, uchar, j-1, (i-1)*3 +2);
(uchar) CV_IMAGE_ELEM( color_imgSelected, uchar, j-1, (i-1)*3)= blue;
(uchar) CV_IMAGE_ELEM( color_imgSelected, uchar, j-1, (i-1)*3+1)= green;
(uchar) CV_IMAGE_ELEM( color_imgSelected, uchar, j-1, (i-1)*3+2)= red;
}
}

into C# Emgu code. I wrote:

for (int i = 1; i < mask.Width - 1; i++)
for (int j = 1; j < mask.Height - 1; j++)
{
Gray idx = mask[j, i];
if (idx.Intensity == 255)
{
imageSelected[j - 1, (i - 1)] = originalImage[j - 1, (i - 1)];
}

}

It works, but it is very very slow!!!
It seems that accessing pixels takes a lot of time. Instead in Opencv this operation is much more fast!!
Or I do something wrong!

Any help?
Thank you very very much!!!

Scissorhands
Posts: 3
Joined: Fri Sep 26, 2008 4:14 am

Re: how can i access to element of an image and modify it

Post by Scissorhands » Fri Sep 26, 2008 9:52 am

I solved the problem in this way:

MCvScalar indx = CvInvoke.cvGet2D(mask, j, i);
if (indx.v0 == 255)
{
MCvScalar newColor = CvInvoke.cvGet2D(originalImage, j - 1, i - 1);
CvInvoke.cvSet2D(imageSelected, j - 1, i - 1, newColor);
}

inside the 2 for.
Now it works very fast!!

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

Re: how can i access to element of an image and modify it

Post by canming » Sun Sep 28, 2008 9:42 pm

If mask contain only values of either 0 or 255, the following call would be THE FASTEST (and use fewest code):

Code: Select all

imageSelected = originalImage.Copy(mask);
If the mask contains values other than 0 or 255, then apply threshold on the mask first.

Post Reply