How to binarize image, Matlab - matlab

I have to binarize image so I have twice as many white pixels as black pixels.
Someone gave me answer:
binarized = im2bw(image, 0.28)
and I'm not sure how do this person know that level 0.28 gives twice as many white as black?
In this code, I had to use gamma correction, use imhist, binarize. My code:
close all;
clear all;
clc;
img = imread('cameraman.tif');
img = double(img)/255;
coeff = 0.6;
gamma = img.^coeff;
figure;
subplot(121); imshow(img); title('Oryginalny');
subplot(122); imshow(gamma); title('Po korekcji gamma');
equalized = histeq(gamma,32);
figure;
subplot(221); imshow(gamma); title('Po korekcji gamma');
subplot(222); imshow(equalized); title('Wyrównany');
subplot(223); imhist(gamma); title('Po korekcji gamma');
subplot(224); imhist(equalized); title('Wyrównany');
SE = strel('disk', 3);
eroded = imerode(equalized,SE);
opening = imdilate(eroded,SE);
figure;
subplot(121); imshow(equalized); title('Wyrównany');
subplot(122); imshow(opening); title('Otwarcie');
binarized = im2bw(opening, 0.28);
figure;
imshow(binarized); title('Po binaryzacji');
w = binarized == 1;
b = binarized == 0;
biale = sum(w(:));
czarne = sum(b(:));

Related

Boundary to binary image - Matlab

In this code I extract the outline of an object. Is there a way to convert it to a fill holes binary image?
Code:
clc;
clear;
close all;
url='https://vemaps.com/uploads/img/it-07.png';
I = imread(url);
imshow(I);
hold on;
BW = imbinarize(I);
[B,L] = bwboundaries(BW,'noholes');
k=1;
stat = regionprops(I,'Centroid');
b = B{k};
c = stat(k).Centroid;
yBoundary = b(:,2);
xBoundary = b(:,1);
cy = c(:,2);
cx = c(:,1);
plot(yBoundary, xBoundary, 'g', 'linewidth', 2);
This is the image outcome I look for:

Plot the MajorAxisLength and the MinorAxisLength - Matlab

I found this script below in https://blogs.mathworks.com/steve/2010/07/30/visualizing-regionprops-ellipse-measurements/
I wish to plot also the MajorAxisLength and the MinorAxisLength, how can I do it?
Script:
url = 'https://blogs.mathworks.com/images/steve/2010/rice_binary.png';
bw = imread(url);
imshow(bw)
s = regionprops(bw, 'Orientation', 'MajorAxisLength', ...
'MinorAxisLength', 'Eccentricity', 'Centroid');
imshow(bw)
hold on
phi = linspace(0,2*pi,50);
cosphi = cos(phi);
sinphi = sin(phi);
for k = 1:length(s)
xbar = s(k).Centroid(1);
ybar = s(k).Centroid(2);
a = s(k).MajorAxisLength/2;
b = s(k).MinorAxisLength/2;
theta = pi*s(k).Orientation/180;
R = [ cos(theta) sin(theta)
-sin(theta) cos(theta)];
xy = [a*cosphi; b*sinphi];
xy = R*xy;
x = xy(1,:) + xbar;
y = xy(2,:) + ybar;
plot(x,y,'r','LineWidth',2);
end
hold off
You have a lot of unnecessary code in what you posted. I don't think you actually attempted to adjust it for what you say you want to plot. Given what you said you are trying to do in the comments, I think this is the code you want in order to plot the Major Axis Length vs. Minor Axis Length
url = 'https://blogs.mathworks.com/images/steve/2010/rice_binary.png';
bw = imread(url);
s = regionprops(bw, 'Orientation', 'MajorAxisLength', ...
'MinorAxisLength', 'Eccentricity', 'Centroid'); %Get region props
Major=zeros(size(s));
Minor=zeros(size(s));
for k = 1:length(s)
Major(k)= s(k).MajorAxisLength; %get your y values
Minor(k)= s(k).MinorAxisLength; %get your x values
end
figure %create a new figure because you don't want to plot on top of the image
plot(Minor, Major, 'o') %plot
xlabel('Minor Axis Length')
ylabel('Major Axis Length')

How to answer this Matlab image processing homework?

I'm stuck here, I tried many times but unable to get my final answer.
Code:
I = imread('C:\Users\Ahsan\Desktop\pears.png');
H = fspecial('average', [3 3]);
J = imfilter(I, H);
figure, imshow(I);
figure, imshow(J);
Try this one:
(Maybe you have to change the threshold);
threshold = 126;
image = imread('C:\Users\Ahsan\Desktop\pears.png');
% Apply the filder
filterImage = conv2(image, ones(3)/9, 'same');
% Check which pixels are equal or greater than the threshold
masked = filterImage >= threshold;
% Replace all pixels of the filteredImage which are below the threshold
% with the original pixels.
filterImage(~masked) = image(~masked);
% Display result
figure(1);
subplot(1,2,1);
imshow(image, []);
subplot(1,2,2);
imshow(filterImage, []);

How can I count no. of holes in an image and measure their diameter using matlab morphological operators?

So I have a graylevel image that demonstrates an electronic circuit card and I'm supposed to inspect the number of holes and the diameter of the holes, and I'm also allowed to use morphology operators in Matlab. The image is as follows:
I could wrote some codes that can count number of holes, but I don't know how to measure their diameters!
clear; close all; clc; warning off
im = imread('input\pcb.jpg');
im1 = im2bw(im,0);
% im1 = ~im2bw(im,0);
figure; imshow(im1);
strel1 = strel('disk',2);
im2 = imclose(im1,strel1);
figure; imshow(im2);
im3 = imfill(im2,'holes');
figure; imshow(im3);
im4 = im3 & ~im1;
figure; imshow(im4);
strel2 = strel('disk',3);
im5 = imopen(im4,strel2);
figure; imshow(im5);
[~,numCC] = bwlabel(im5);
fprintf('Number of holes equals:\t%d\n',numCC);
I appreciate any comments in advance!
Finally I just wrote some code, and it seems that it's working somehow perfect!
Actually the number of holes are counted as 4 and their diameters are not precise ones but they're approximated using built-in MATLAB functions. The thing is that one of the holes is not separated distinctly! and it makes the results estimated ...
clear; close all; clc; warning off
im = imread('input\pcb.jpg');
level = graythresh(im);
imBin = im2bw(im,level);
figure(1); imshow(imBin); title('Binarized Original Image');
imBinInv = ~imBin;
figure(2); imshow(imBinInv); title('Inverted Binarized Original Image');
imInvHolSep = imdilate(imerode(imBinInv,strel('disk',21)),strel('disk',23));
figure(3); imshow(imInvHolSep); title('Inverted Holes Separated');
imInHolSepBound = imInvHolSep & ~imerode(imInvHolSep,strel('disk',2));
figure(4); imshow(imInHolSepBound); title('Inverted Holes Boundaries');
imInvHolSepFill = imfill(imInHolSepBound,'holes');
figure(5); imshow(imInvHolSepFill); title('Inverted Holes Filled After Setting Boundaries');
imInvHolSepDist = imerode(imInvHolSepFill,strel('disk',1));
figure(6); imshow(imInvHolSepDist); title('Inverted Holes Eroded Just For The Case of Indistinct Hole');
imInvHolSepMinus = imInvHolSepDist & ~imBin;
figure(7); imshow(imInvHolSepMinus); title('Inverted Holes Minus The Inverted Binarized Image');
imInvHolSepSmooth = imdilate(imInvHolSepMinus,strel('disk',2));
figure(8); imshow(imInvHolSepSmooth); title('Final Approximated Inverted Holes Smoothed');
[~,numCC] = bwlabel(imInvHolSepSmooth);
fprintf('Number of holes equals:\t%d\n',numCC);
stats = regionprops(imInvHolSepSmooth);
centroid = zeros(length(stats),2);
area = zeros(length(stats),1);
for c1 = 1:length(stats)
centroid(c1,:) = stats(c1).Centroid;
area(c1) = stats(c1).Area;
fprintf('Diameter of the hole with centroid coordinates [%.2f, %.2f] is:\t%.2f\n',centroid(c1,1),centroid(c1,2),sqrt(area(c1)/pi));
end

Convolving two images

This is my coding for convolving my image and patch of 16*16 of same image.I changed my coding now.But I am getting only image full of white pixels? This is my changed coding
clear all
close all
I=imread('E:\double.jpg');
subplot(3,2,1);
imshow(I);
title('Original');
%gray conversion
J = rgb2gray(I);
subplot(3,2,2);
imshow(J);
title('Gray');
K = double(J);
%size of the image
[A B]=size(J);
%patch definition
patch_size = 16;
temp = J(1:patch_size, 1:patch_size);
subplot(3,2,3);
imshow(temp);
title('kernel');
temp1 = double(temp);
C = conv2(temp1,K);
subplot(3,2,4);
imshow(C);
title('Convolved image');