Otsu's Threshold Question

4 views (last 30 days)
I am attempting to create a classifier which detects the solar panels in a series of images. To create the training data i have been using Otsu's threshold to seperate the blue solar cells from the white grids (see first image below). I have been able to isolate the solar panels from the background, as seen in the second image, and then use the threshold to seperate the classes. I, however, want the blue cells to be highlighted instead of the white grid lines. Does anyone know how to do this? Below is also the code I have been running and the output images at various points.
RGB = imread('1-108.png'); %input image (as seen above)
GT = imread('1-108gt.png'); %original groundtruth of input image which highlights both grid and cell
I = rgb2gray(RGB); %convert RGB to intensity
[row,col] = size(I);
com = uint8(zeros(size(I)));
%when the groundtruth is logic 1, replace this with the corresponding intensity pixel
for r=1:row
for c=1:col
if GT(r,c)==1
com(r,c) = I(r,c);
else
com(r,c) = 0;
end
end
end
figure
imshow(com)
[counts, x] = imhist(com,16);
figure
stem(x,counts)
T = otsuthresh(counts(2:16,:)); %does not consider the first column as this is all the background pixels
BW = imbinarize(com,T);
figure
imshow(BW)

Accepted Answer

Matt J
Matt J on 4 Sep 2021
Edited: Matt J on 4 Sep 2021
I= double(rgb2gray(imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/729084/1-108.png')));
GT = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/729089/1-108gt.png');
com= uint8((GT==1).*I);
[counts, x] = imhist(com,16);
T = otsuthresh(counts(2:16,:)); %does not consider the first column as this is all the background pixels
BW =GT-imbinarize(com,T);
BW=imclose(BW,ones(3,1));
BW=imfill(BW,'holes');
BW=imopen(BW,ones(5,1));
BW=imopen(BW,ones(1,3));
imshow(BW)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!