how to scan the first black pixel of top left on image

hello there, i want to scan the first black pixel of top left on image and store the coordinate of that pixel. how can i do.. here i attach example of image.

Answers (2)

row = find( any(~YourImage, 2), 1);
col = find(~YourImage(row,:), 1);

3 Comments

There's no guarantee that a pixel at (row,col) is actually black. If the image has even the slightest tilt or a scratch of noise outside the border, these two lines of code will find different pixels, and the resulting pixel location will not be the top-left corner.
True, but I think that mohd wants to shift the image so that the left most part of the black frame is in column 1, and the top most part of the black frame gets shifted to row 1 (he mentioned that in hist prior post). Walter's code finds the bounding box of the black frame, even if it's tilted, and so I think that is exactly what mohd needs. I don't think he really needs the upper left corner despite the fact that he asked for that. (If he really did want the upper left corner you might have to use regionprops of find and examine all coordinates and find how which had the shortest distance to row 1, column1.) But like I said, I don't think that is really what he requires if he just wants to shift the image to the upper left, as he mentioned in his prior post where I recommended circshift(). Now he's wanting to know how much shift to pass into circshift, and Walter's code will give that.

Sign in to comment.

If your image data is grayscale or indexed (1 value per pixel), you can do this:
[y,x] = find( img == 0, 1 );
If it's RGB, you can do the zero-test in each channel:
[y,x] = find( all(img==0, 3), 1 );
This assumes pure-black.

2 Comments

Geoff, that find() line will go down the columns first. If the picture is tilted towards the right, that would find the bottom left corner instead of the top left. The code I gave in my answer finds the top row first and then the first column in the top. Mind you, that has the opposite problem, finding the upper right corner if the picture is tilted towards the left.
Hmm, true. You might want to change your code to find the zero pixels ;-) I suppose a solution then would be to find all the [x,y] coords for black pixels and then do:
cornerDist = hypot(x,y)
iCorner = find( cornerDist == min(cornerDist), 1 );
x = x(iCorner);
y = y(iCorner);

Sign in to comment.

Asked:

on 15 Apr 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!