I'm detecting circles from an image using imfindcircles function.
Below is the code.
image here
img= imread('image.png');
imshow(img);
rmin=10
rmax=50
[centersDarkl, radiiDarkl]=imfindcircles(img,
[rmin,rmax],'ObjectPolarity','dark','Sensitivity',0.80);
viscircles(centersDarkl, radiiDarkl,'LineStyle','--')
Now, I want to crop the detected circles and save them as different figures.
Here is a solution that works for this image. I used floor and ceil to avoid the edges and rmax had to be larger than 75.
[img,map] = imread('MwBQo.png','png');
img = ind2rgb(img, map);
figure;
imshow(img);
rmin = 10;
rmax = 80;
[centersDarkl, radiiDarkl] = imfindcircles(img,...
[rmin,rmax],'ObjectPolarity','dark','Sensitivity',0.80);
viscircles(centersDarkl, radiiDarkl,'LineStyle','--')
for iCirc = 1:size(centersDarkl,1)
cropped{iCirc,1} = img(...
ceil(centersDarkl(iCirc,2)-radiiDarkl(iCirc)):...
floor(centersDarkl(iCirc,2)+radiiDarkl(iCirc)),...
ceil(centersDarkl(iCirc,1)-radiiDarkl(iCirc)):...
floor(centersDarkl(iCirc,1)+radiiDarkl(iCirc)),:);
end
figure;
subplot(1,2,1)
imshow(cropped{1})
subplot(1,2,2)
imshow(cropped{2})
Related
I am trying to outline all peaks in an image. The brightest lines are the peaks. I am using Matlab. This is what I have so far....
Any help will be greatly appreciated. Here is the image.
a = imread('duneLiDARs.png');
%b = imregionalmax(a);
%a = rgb2gray(a);
c = edge(a,'Sobel');
b = edge(a,'log',.0006);
d = edge(a,'log');
c= imfuse(a,d);
d= d-b;
subplot(2,2,1), imshow(a)
subplot(2,2,2), imshow(b)
subplot(2,2,3), imshow(c)
subplot(2,2,4), imshow(d)
%imshow(b);
%c = imadd(a,b);
%imshow(b);
you need to define what do you consider as peaks - what is the desired output for your image.
however, there are some general 2D peaks finding function, the following code uses FEX's extrema2:
% load image and remove extreme noise
im = medfilt2( im2double(imread('dune.png')));
% find peaks using extrema2
[XMAX,IMAX,XMIN,IMIN] = extrema2(im);
% eliminate peaks under minimum threshold
underThresh = XMAX < 0.15;
IMAX(underThresh) = [];
XMAX(underThresh) = [];
% plotting
subplot(121);
surf(im,'EdgeColor','none');
hold on;
[y,x] = ind2sub(size(im),IMAX);
scatter3(x,y,XMAX,'r','filled');
axis square
subplot(122);
imshow(im,[]);
hold on;
scatter(x,y,'r','filled');
As this picture, how to use matlab code to split it into different parts, and then fill color in it? In addition, how to set gradient color in the second code ???
Here is the picture segmentation code:
clc
rgb=imread('sample1.bmp');
bw=im2bw(rgb2gray(rgb),.8);
bw=medfilt2(bw);
planes=bwareaopen(bw,800);
D=mat2gray(bwdist(imcomplement(planes)));
stats=regionprops(D>.8,'Centroid');
planes_centroid=cat(1,stats.Centroid);
planes_mask=false(size(bw));
planes_mask(sub2ind(size(bw),round(planes_centroid(:,2)),...
round(planes_centroid(:,1))))=1;
M=imimposemin(imcomplement(D),planes_mask);
L=watershed(M);
r=L & planes;
stats=regionprops(r,'BoundingBox','Centroid')
bb=cat(1,stats.BoundingBox);
c=cat(1,stats.Centroid);
figure,imshow(planes)
hold on
for i=1:length(stats)
rectangle('Position',bb(i,:),'EdgeColor','b')
plot(c(i,1),c(i,2),'r*')
text(c(i,1)-5,c(i,2)-10,num2str(i))
end
%second code
clc;clf;close all;clear all;
color=cell(4,1);
for i=1:4
input=imread(['heartspline2_4_',num2str(i)],'bmp');
figure,imshow(input);
BW=im2bw(input,graythresh(input));
[B,L]=bwboundaries(BW,'noholes');
for k=1:length(B)
boundary=B{k};
ind=size(boundary(:,2));
plot(boundary(:,2),boundary(:,1),'k','LineWidth',2);
hold on;
axis off;
if (k==1)
patch(boundary(:,2),boundary(:,1),'w');
else
patch(boundary(:,2),boundary(:,1),???);
end
end
saveas(gca,['y_','heartspline2_4_',num2str(i)],'bmp')
close(gcf)
end
You can use bwlabel to assign different index to each image region:
img = imread('http://i.stack.imgur.com/F1Iya.jpg'); %// read image
bw = img(:,:,1) > 128; %// convert to binary mask
lb = bwlabel(bw,4); %// extract distinct regions
The result:
figure; imshow(lb, [], 'border', 'tight'); colormap(rand(256,3));
If you want a gradient effect to the colors, you can
[x y] = meshgrid(linspace(0,1,size(bw,2)), linspace(0,1,size(bw,1)));
rand('seed',543310);
rgb_lb = ind2rgb(lb, rand(max(lb(:)+1),3)); %// convert to RGB color image
gx = x;
gx(lb==1)=1; %// use the horizontal gradient
gx = gx./max(gx(:));
Apply the gradient:
rgb_lb = bsxfun(#times, rgb_lb, gx);
The result:
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
I am trying to figure out how to draw rectangles on an image in Matlab.
Once the rectangles are drawn on the image I would like to save the changes.
Thanks in advance!
Use getframe
img = imread('cameraman.tif');
fh = figure;
imshow( img, 'border', 'tight' ); %//show your image
hold on;
rectangle('Position', [50 70 30 60] ); %// draw rectangle on image
frm = getframe( fh ); %// get the image+rectangle
imwrite( frm.cdata, 'savedFileName.png' ); %// save to file
See rectanlge for more options on drawing rectangles. The 'Position' argument for rectangle is in format [from_x from_y width height] and is given in units of pixels.
Without using getframe:
im=imread('face.jpg'); %Image read
rectangle('Position', [10 10 30 30] ,...
'EdgeColor', 'r',...
'LineWidth', 3,...
'LineStyle','-');%rectangle properties
imshow( im, rectangle); %draw rectangle on image.
For more detail visit this MathWorks thread :)
The best and easy way to use is in the updated Matlab versions
img = imread('abcd.jpg');
box = [X1, Y1, width, height];
outImage= insertObjectAnnotation(img,'rectangle',bbox,'Rectangle');
figure, imshow(outImage), title('Image with rectangle');
I am using octave and the function getframe is not available so i wrote this trivial function
%% Draw red rectangle IN the image using the BoundingBox from regionprops
function rgbI = drawRectangleOnImg (box,rgbI)
x = box(2); y = box(1); w = box(4); h = box(3);
rgbI(x:x+w,y,1) = 255;
rgbI(x:x+w,y+h,1) = 255;
rgbI(x,y:y+h,1) = 255;
rgbI(x+w,y:y+h,1) = 255;
rgbI(x:x+w,y,2) = 0;
rgbI(x:x+w,y+h,2) = 0;
rgbI(x,y:y+h,2) = 0;
rgbI(x+w,y:y+h,2) = 0;
rgbI(x:x+w,y,3) = 0;
rgbI(x:x+w,y+h,3) = 0;
rgbI(x,y:y+h,3) = 0;
rgbI(x+w,y:y+h,3) = 0;
end
I=imread('%required image');
[M,N] = size(rgb2gray(I));%find the size of the image
x=int16(M/3);y=int16(N/3);xwidth=int16(N/3); ywidth=int16(N/3);%specify the position
pos=[x y xwidth ywidth];% give the position in which you wanna insert the rectangle
imshow(I);hold on
rectangle('Position',pos,'EdgeColor','b')
I would like to overlay two images in MATLAB (I and imagesc(data)) and then draw a rectangle on top of those. I2 specifies the transparency pattern in the following code. The rectangle becomes a line on top of the image. Can anyone tell me why the rectangle is not drawn correctly?
imshow(I);
hold on;
h = imagesc(data,[0,1]);
hold off;
I2 = ones(height,width) * 80;
set(h, 'AlphaData', I2);
rectangle('Position',[100,100,20,20]);
Since we can't reproduce your code exactly without all the data, here is a complete example with sample images:
%# some sample images
I = imread('coins.png');
I_transp = imread('peppers.png');
%# create a gaussian mask for transparency
[r,c,~] = size(I_transp);
M = fspecial('gaussian', [r c], mean([r c]./5));
M = (M-min(M(:)))./range(M(:));
%# show overlayed images
figure, imshow(I, 'XData',[1 c], 'YData',[1 r]), hold on
hImg = imshow(I_transp);
set(hImg, 'AlphaData',M);
%# draw a rectangle
rectangle('Position',[355 220 100 100], 'LineWidth',2, 'EdgeColor','b');