Thread Subject: Crop/change colour of part of an image

Subject: Crop/change colour of part of an image

From: Kenneth Galea

Date: 21 Mar, 2010 16:24:02

Message: 1 of 9

Hi everyone,

          I have a black image (640x480) and want to select a area square/rectangle (of this image and make it white. Then I need to display the black image again with the white patch on it. I read about imcrop and imsubtract but still no clue. any help please??

Thanks
kenneth

Subject: Crop/change colour of part of an image

From: Kenneth Galea

Date: 21 Mar, 2010 16:39:04

Message: 2 of 9

"Kenneth Galea" <k.galea@hotmail.com> wrote in message <ho5h72$sav$1@fred.mathworks.com>...
> Hi everyone,
>
> I have a black image (640x480) and want to select a area square/rectangle (of this image and make it white. Then I need to display the black image again with the white patch on it. I read about imcrop and imsubtract but still no clue. any help please??
>
> Thanks
> kenneth
Sorry forgot to mention. The image is not always black but the patch on the image needs to be always white.
Thanks
Kenneth

Subject: Crop/change colour of part of an image

From: ImageAnalyst

Date: 21 Mar, 2010 17:26:37

Message: 3 of 9

Kenneth Galea
Use imcrop (I'm sure you'll figure it out) and then use poly2mask().

Or use imrect() and the createMask() method of imrect().

Subject: Crop/change colour of part of an image

From: Kenneth Galea

Date: 21 Mar, 2010 19:53:04

Message: 4 of 9

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <98732dee-2af0-4941-a72e-275045cc8b73@y17g2000yqd.googlegroups.com>...
> Kenneth Galea
> Use imcrop (I'm sure you'll figure it out) and then use poly2mask().
>
> Or use imrect() and the createMask() method of imrect().


Sorry to ask again. But I can't understand the purpose of imcrop if I use poly2mask() since from what I can see poly2mask(x,y,m,n) needs 4 input arguments where x and y are where pixels inside x and y are set to 1 while the rest are set to 0. here's my code :

white = imread('C:\Users\Kenneth\Desktop\white.jpg');
f = imread('C:\Users\Kenneth\Desktop\black.jpg');
size (f)
whos f
[m,n] = size (f);
X=m;
Y=n/3;

rect = [200 12 240 100] %selected area to crop
I2 = imcrop(f, rect) %crop specified black area
figure
imshow (I2) %show cropped area

x = [0 186 54 190];
y = [0 100 209 500];

BW = poly2mask(x, y, X, Y)
figure
imshow (BW)

I can't find a connection between rect and x,y & f and X, Y :/

Thanks
Kenneth

Subject: Crop/change colour of part of an image

From: ImageAnalyst

Date: 21 Mar, 2010 20:50:28

Message: 5 of 9

Kenneth Galea
imcrop just allows you to interactively set the bounding box. From
your code, it appears that you already know the coordinates of the
region to crop. In this case, you can simply use those coordinates
and not bother to ask the user where to crop. imcrop will give you
back a cropped grayscale image if you pass it a larger grayscale
image. poly2mask() will give you a binary image, unlike the grayscale
image that imcrop gives you. So if you want a binary mask, you need
to use poly2mask().

The 3rd and 4th arguments of poly2mask say what size you want the
output image to be. For example you might want a small box inside of
a much larger image. It appears that you are saying that you want the
mask image to be the same number of rows (X=m=#rows) as f (your badly-
named original image), and you want the number of columns (which
likewise you're deceptively calling Y) to be 1/3 the number of columns
as your original image. I have no idea why you're doing that. Your
"BW" is only 1/3 as wide as your original "f" image. Usually masks
are the same size as the image, but perhaps you have plans to apply
that mask to an as-of-yet-undefined image that's only 1/3 as wide as
your "f" image.

Finally, I have no idea where you're getting your x and y from - they
don't seem to be related to your cropping rectangle at all - you just
hard code them in. How did you come up with these? Why did you
crop? Is your mask supposed to have anything at all to do with your
cropping box?

Subject: Crop/change colour of part of an image

From: Kenneth Galea

Date: 21 Mar, 2010 21:33:02

Message: 6 of 9

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <80222c2b-7d35-42d7-983c-4ed2bb1dbf7d@m37g2000yqf.googlegroups.com>...
> Kenneth Galea

No those values were for testing purposes only!!
Just to be clear and precise. I have a camera which captures an image with a white background (640x480). I also have a robotic arm (which is outside this image border in order to not interfere i.e. be recognised as an object when the image is taken). Due to this reason since my robotic arm is small it is not reaching that far as I wish and thus I cannot use the maximum out of it. Thus my idea was to move the robotic arm more close to the border (resulting in a small area which would be lost due to cropping the part were the arm can be seen but a much larger area gained on the other hand due to more length achieved).

Now what I actually require is that top middle of the (640x480) image captured (e.g. a box with an area of 100 by 100) would be always set as white such that the robotic arm would not be recognised as an object even though it makes part of the image!!
Hope I'm clear

Thanks
Kenneth

Subject: Crop/change colour of part of an image

From: Kenneth Galea

Date: 21 Mar, 2010 21:49:04

Message: 7 of 9

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <98732dee-2af0-4941-a72e-275045cc8b73@y17g2000yqd.googlegroups.com>...
> Kenneth Galea
> Use imcrop (I'm sure you'll figure it out) and then use poly2mask().
>
> Or use imrect() and the createMask() method of imrect().


Sorry to ask again. But I can't understand the purpose of imcrop if I use poly2mask() since from what I can see poly2mask(x,y,m,n) needs 4 input arguments where x and y are where pixels inside x and y are set to 1 while the rest are set to 0. here's my code :

white = imread('C:\Users\Kenneth\Desktop\white.jpg');
f = imread('C:\Users\Kenneth\Desktop\black.jpg');
size (f)
whos f
[m,n] = size (f);
X=m;
Y=n/3;

rect = [200 12 240 100] %selected area to crop
I2 = imcrop(f, rect) %crop specified black area
figure
imshow (I2) %show cropped area

x = [0 186 54 190];
y = [0 100 209 500];

BW = poly2mask(x, y, X, Y)
figure
imshow (BW)

I can't find a connection between rect and x,y & f and X, Y :/

Thanks
Kenneth

Subject: Crop/change colour of part of an image

From: Kenneth Galea

Date: 21 Mar, 2010 22:30:08

Message: 8 of 9

"Kenneth Galea" <k.galea@hotmail.com> wrote in message <ho63ae$hq9$1@fred.mathworks.com>...
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <80222c2b-7d35-42d7-983c-4ed2bb1dbf7d@m37g2000yqf.googlegroups.com>...
> > Kenneth Galea

This is something similar to what I need. I found this link:

http://blogs.mathworks.com/steve/2007/02/24/roipoly-rectangular-regions-and-logical-indexing/

Now considering the last image shown in this link, I need that "polygon" which in my case is a square to be completely white

Thanks
Kenneth

Subject: Crop/change colour of part of an image

From: ImageAnalyst

Date: 21 Mar, 2010 23:42:36

Message: 9 of 9

Then why don't you just say
noRobotArmImage = originalImageWithArm; % Initialize as the original
image.
noRobotArmImage(1:100, 190:289) = 255; % White out the part with the
robot arm in it.
Or set it to the white background gray level - whatever it takes to
make that portion not be considered by your subsequent processing (be
it thresholding, or whatever).

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
image processing Kenneth Galea 21 Mar, 2010 12:29:07
rssFeed for this Thread

Contact us at files@mathworks.com