I have a RGB image. I have scanned the image. So the image occupies a small portion of an A4 size sheet.
I want to find the border of the image and crop it. I could use edge detection operators like 'Sobel' etc, but they detect all the edges present in the image. All I want is the border of the image. Also many of the edge detection functions including 'bwboundaries' work only with binary or grayscale images. My image is RGB.
I tried using 'imcrop', but this is more of interactive cropping. I am keen on doing this automatically.
Uploading a test image:
Since this is an rgb image, there will be apparent color in the gray areas, but there should be none in the white ones. You can make use of this to find the image, then you can get the bounding box.
There is a bit of a border left due to rotation. You can use the mask
tto
above to set all non-image pixels to NaN before cropping, or you can useimrotate
to fix your image.You can try using
bwlabel
http://www.mathworks.com/help/toolbox/images/ref/bwlabel.html (along with find, as noted in the help page) to get the indices of the image and use those to crop the original.You'll first need to convert the original image to binary using
im2bw
http://www.mathworks.com/help/toolbox/images/ref/im2bw.html.You can try to detect the corners of your image using e.g. the Harris-Detector (
corner
in Matlab). Set the maximum number of corners to detect to 4. Then use the positions of the corners inimcrop
. If you would post an image I could give you more specific hints. Your image being RGB shouldn't be a problem, just convert it to grayscale.