Find biggest contour for shadow detection - matlab

I have this problem in image processing and I couldn't find an algorithm to perform well under this condition.It's so simple to understand but I don't know how to implement it in ‍‍OpenCV or in Matlab, so any algorithm or function in one of them (MATLAB or opencv) is helpful.
1 . lets suppose that an image and background of scene is like the below
2 . We apply an edge detector to image and my current image will be like the below picture.
now my Problem is that how we can find the biggest contour or area like the below in edge image?
If you want original picture the original picture is
and in matlab you can get edge image by below codes.
clc
clear
img = imread('1.png'); % read Image
gray = rgb2gray(img); % Convert RGB to gray-scale
edgeImage = edge(gray,'canny',0.09); % apply canny to gray-scale image
imshow(edgeImage) % Display result in figure(MATLAB)
In OpenCV you can use below code
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat img = imread("1.png");
Mat gray;
cvtColor(img, //BGR form image
gray, //Mat for gray(destination)
CV_BGR2GRAY); //type of transform(in here BGR->GRay)
Mat edgeImage;
Canny(gray, //Input Array
edgeImage, //Output Array
40, // Lower threshold
120); //Upper threshold
namedWindow("Edge-Image"); //create a window for display image
imshow("Edge-Image",edgeImage); //Display edgeImage in window that in before line create
waitKey(0); //stick display window and wait for any key
return 0;
}

Here is a solution in Matlab using imdilate to close the contours and regionprops to get the closed objects area:
% Your code to start
img = imread('Image.png'); % read Image
gray = rgb2gray(img); % Convert RGB to gray-scale
edgeImage = edge(gray,'canny',0.09); % apply canny to gray-scale image
% First dilate to close contours
BW = imdilate(edgeImage, strel('disk',4,8));
% Then find the regions
R = regionprops(~BW, {'Area', 'PixelIdxList'});
% Find the second biggest region (the biggest is the background)
[~, I] = sort([R(:).Area], 'descend');
Mask = zeros(size(img));
Mask(R(I(2)).PixelIdxList) = 1;
% Display
clf
imshow(Mask)
An the result is:
Best,

First close the contour with morphological closing since you can't find it now as it is not really a distinct contour, but a part of the larger one.
After closing, just use the findContours() function and use its output to get the area of each contour and eventually find the maximum one by using the contourArea() function.

Related

Image single background color - Matlab

In the code below I rotated an image. How can I get a single background color (white or black)?
code:
close all;
clear;
clc;
url='http://www.clker.com/cliparts/T/i/o/c/X/Q/airplane-md.png';
RI = imread(url);
I = rgb2gray(RI);
BI = imbinarize(I);
LI = bwlabel(BI);
mea = regionprops(LI, 'All');
RI = imrotate(RI, -mea(1).Orientation,'loose');
imshow(RI);
Given that the image is a simple logo (as opposed to a photo for instance) you can likely use logical indexing to change all of the black pixels added by imrotate to white pixels.
I don't have the image processing toolbox so I couldn't run your code, but the sample below should illustrate:
%Load RBG image to test on
RI = imread('peppers.png');
%Create black region to remove
RI(100:150,100:150,:) = 0;
figure()
imshow(RI)
title('Original Image')
%Replace all black pixels with white
inds = sum(RI,3)==0;
RI_new = RI;
RI_new(repmat(inds,1,1,3))=255;
figure()
imshow(RI_new)
title('New Image')
In comparison to the answer from #SardarUsama, this has the weakness of assuming there are no black pixels in your original image but the advantage of using built-in Matlab functions only.
Edit: Updated to show example on RGB image rather than grayscale
Your original image has white background. When you rotate it, you get black pixels in the background to fill up the image matrix. This may be due to that the preallocation of the rotated image matrix is done with zeros which then translates to black (implemented probably in imrotatemex and in the lines 116 and 118 of imrotate). You can use these alternate implementations of imrotate but preallocate the matrix with ones (for double data) or 255 (for uint8 data).
For example, in line 31 of Rody's implementation, i.e.:
imagerot = zeros([max(dest) p],class(image));
Change this line to:
imagerot = 255*ones([max(dest) p],'uint8'); %Your image is uint8 in this case
Result:

Contrast Stretching for colore images with MATLAB

i'm working in matlab and i wanted to apply the Contrast Stretching for grey scale image and also RGB image ,
so for the grey scale i've tried this one and it worked
clear all;
clc;
itemp = imread('cameraman.tif'); %read the image
i = itemp(:,:,1);
rtemp = min(i); % find the min. value of pixels in all the
columns (row vector)
rmin = min(rtemp); % find the min. value of pixel in the image
rtemp = max(i); % find the max. value of pixels in all the
columns (row vector)
rmax = max(rtemp); % find the max. value of pixel in the image
m = 255/(rmax - rmin); % find the slope of line joining point
(0,255) to (rmin,rmax)
c = 255 - m*rmax; % find the intercept of the straight line
with the axis
i_new = m*i + c; % transform the image according to new slope
figure,imshow(i); % display original image
figure,imshow(i_new); % display transformed image
this is for greyscale image ,
the problem is that that i don't know how to do for the RGB image
any idea? how to implement that?
thank you :)
Could the function stretchlim (reference) be useful for your purpose?
Find limits to contrast stretch image.
Low_High = stretchlim(RGB,Tol) returns Low_High, a two-element vector
of pixel values that specify lower and upper limits that can be used
for contrast stretching truecolor image RGB.
img = imread('myimg.png');
lohi = stretchlim(img,[0.2 0.8]);
If you write
rmin = min(i(:));
Then it computes the minimum over all values in i. This will work for RGB images also, which simply are 3D matrices with 3 values along the 3rd dimension.
The rest of your code also applies directly to such images.

Watershed Algorithm setting removing all connected components

I'm using the watershed algorithm to try and segment touching nuclei. A typical image may look like:
or this:
I'm trying to apply the watershed algorithm with this code:
show(RGB_img)
%Convert to grayscale image
I = rgb2gray(RGB_img);
%Take structuring element of a disk of size 10, for the morphological transformations
%Attempt to subtract the background from the image: top hat is the
%subtraction of the open image from the original
%Morphological transformation to subtract background noise from the image
%Tophat is the subtraction of an opened image from the original. Remove all
%images smaller than the structuring element of 10
I1 = imtophat(I, strel('disk', 10));
%Increases contrast
I2 = imadjust(I1);
%show(I2,'contrast')
%Assume we have background and foreground and assess thresh as such
level = graythresh(I2);
%Convert to binary image based on graythreshold
BW = im2bw(I2,level);
show(BW,'C');
BW = bwareaopen(BW,8);
show(BW,'C2');
BW = bwdist(BW) <= 1;
show(BW,'joined');
%Complement because we want image to be black and background white
C = ~BW;
%Use distance tranform to find nearest nonzero values from every pixel
D = -bwdist(C);
%Assign Minus infinity values to the values of C inside of the D image
% Modify the image so that the background pixels and the extended maxima
% pixels are forced to be the only local minima in the image (So you could
% hypothetically fill in water on the image
D(C) = -Inf;
%Gets 0 for all watershed lines and integers for each object (basins)
L = watershed(D);
show(L,'L');
%Takes the labels and converts to an RGB (Using hot colormap)
fin = label2rgb(L,'hot','w');
% show(fin,'fin');
im = I;
%Superimpose ridgelines,L has all of them as 0 -> so mark these as 0(black)
im(L==0)=0;
clean_img = L;
show(clean_img)
For whatever reason after C = ~BW; the whole image goes dark. This very same code block has worked on a handful of other images, all of which were more "solid" or not as grainy as these. However, I thought I compensated for this with BW = bwdist(BW) <= 1;. I've experimented a ton and I don't really know what's happening. Any help would be great!
Ps. this is the image after BW = bwareaopen(BW,8);
Before the top-hat, you should perform a closing and an opening in order to reduce the noise.
If you perform an area opening on a noisy image, you may end up with the result on your black and white image.
So it would be:
Closing and opening
Top-Hat
Area opening if still necessary
Thresholding
Erosion and dilation to find the inner and outer markers respectively
Watershed (never use a watershed without markers).

Finding similar corners in matlab

I need to find similar corners in image (for example: 4 corners of a rectangle, same corners, just different orientation?).
I have this code:
% read the image into MATLAB and convert it to grayscale
I = imread('s2.jpg');
Igray = rgb2gray(I);
figure, imshow(I);
% We can see that the image is noisy. We will clean it up with a few
% morphological operations
Ibw = im2bw(Igray,graythresh(Igray));
se = strel('line',3,90);
cleanI = imdilate(~Ibw,se);
figure, imshow(cleanI);
% Perform a Hough Transform on the image
% The Hough Transform identifies lines in an image
[H,theta,rho] = hough(cleanI);
peaks = houghpeaks(H,10);
lines = houghlines(Ibw,theta,rho,peaks);
figure, imshow(cleanI)
% Highlight (by changing color) the lines found by MATLAB
hold on
After running this code I convert my starting image into a binary image with:
binary = im2bw(I);
after this I get a product from those 2 binary images and I think I get corners..
product = binary .* cleanI;
now I imfuse this picture with grayscale starting picture and get this:
I dont know what to do to get only those 4 corners!
Ok second try. Below a code that does not finally do the job but it might help.
Edge identifies the contours and with regionprops you get the characteristica of each identified element. As soon as you know what characteristics your desired object has you can filter it and plot it. I went through the Areas in shapedata.Area and the 6th largest was the one you were searching for. If you combine Area with some of the other charateristica you might get the one you want. As I said not ideal and final but perhaps a start ...
clear all
close all
source = imread('Mobile Phone.jpg');
im = rgb2gray(source);
bw = edge(im ,'canny',[],sqrt(2));
shapedata=regionprops (bwlabel(bw,8),'all');
%index = find([shapedata.Area]== max([shapedata.Area]));
index = 213;
data = shapedata(index).PixelList;
figure
imshow(im)
hold on
plot(data(:,1),data(:,2),'ro');

Fit curve to a region with specific color in Matlab

I am trying to fit a curve on a specific area in an image using the color of the pixels. As it is shown in the image (https://db.tt/PcxHGbT3), there is a region in the image with grey color that can be detected using image processing in Matlab. Once I found the position of the pixels using the following code in Matlab:
im = imread('layer.jpg');
figure,imshow(im);title('Original Image');
[y,x] = find(all(im<100, 3));
I need to find the position of the points sitting on the central line of the region which is shown in the image (https://db.tt/PcxHGbT3). I was thinking to somehow fit a curve, but I have no idea on how I can do this in Matlab. Is there any shorter way other than processing all of the points?
Can you use im2bw(im, 100/256) to threshold the image and bwmorph(BW, 'thin', INF) to thin the result?
You could use matlab binary image operations to get centroids of the points withing the gray area. Having centroids you can do whatever with them, e.g. fitting lines. This is one example based on your image, how this can be done.
% convert rgb to gray scale
im = rgb2gray(imread('layer.jpg'));
% mask of gray-color with points
bwMask1 = im < 100;
% mask with points only
bwMask2 = im < 20;
% remove points outside gray area
bwMask3 = bwareaopen(bwMask1, 400);
% points only withing gray ![enter image description here][1]area
bwMask4 = bwMask2 & ~(bwMask3 - bwMask2);
% label all points
[L] = bwlabel(bwMask4, 8);
% calculate points parameters
pointStats = regionprops(L);
% get centroids of each point
pointCentroidsCell = {pointStats(:).Centroid};
pointCentroidsMat = vertcat(pointCentroidsCell{:});
%plot results:
RGB = label2rgb(L);
figure, imshow(RGB); title('labeled points');
![enter image description here][2]
figure,imshow(im); hold on;
plot(pointCentroidsMat(:, 1), pointCentroidsMat(:, 2), 'r*');
title('Found centroinds');