contour line edge detection in MATLAB [duplicate] - matlab

This question already exists:
contour Detection in MATLAB with GUI
Closed 7 years ago.
I have a project to detect the contour lines in image , but when I run my code with the canny edge detection algorithm , one line in the image transforms to two lines, because of two times the change in grey values of the line before and after that .
i= imread('path');
imgG= rgb2gray(i);
PSF = fspecial('gaussian',7,7);
Blurred = imfilter(imgG,PSF,'symmetric','conv');
figure ,imshow(Blurred)
edgeimg = edge(Blurred , 'canny');
figure ,imshow(edgeimg)
I have no idea to solve this, please help me.

The best answer depends on what you want to do with the edges after detecting them, but let's assume you just want to generate an image where the lines are pure black and everything else is pure white...
The simplest approach is to threshold the image so that lighter grey pixels become white and darker grey pixels become black. You can also erode the image to try and reduce the thickness of lines -- although you'll find that this will get rid of the fine contours in your sample image.
Here is the code to do this (assumes you have the image G4.jpg in your working folder).
% load image and convert to double (0 to 1) as well as flip black and white
imG4 = imread('G4.jpg');
imG4_gs = 1 - mean(double(imG4)/255,3);
figure
image(64*(1 - imG4_gs))
colormap('gray');
axis equal
% image grayscale threshold
img_thr = 0.25;
% apply threshold to image
imG4_thr = imG4_gs >= img_thr;
figure
image(64*(1 - imG4_thr))
colormap('gray');
axis equal
% erode image (try "help imerode" in the MATLAB console)
imG4_ero = imerode(imG4_thr,strel('disk',1));
figure
image(64*(1 - imG4_ero))
colormap('gray');
axis equal;

Related

Removing specific colored edge section of an image using MATLAB

How can I remove the purple colored section from the edge of the image but not from inside of the image using MATLAB. Please suggest me a general code which can be applied future for any kind of this type image.
Main Image
In case you don't get which section is to be removed I marked the section with red color
So, this is what I came with. I hope the code is self explanatory.
There are 2 values you need to play with. One of them is the number in imdilate. That one will define "how big the boundary is". Of course that depends on you.
The other one is the value for HSV color segmentation. In HSV, H is color, and purple is around 250-335 range. The problem is that blue is very similar to purple, and the limit between purple and blue is very ambiguous. I used 250 as lower limit in my code, but you may wan to modify that.
If you have any question please ask.
% The image is indexed image. Else convert.
[img,c]=imread('https://i.imgur.com/dxkJSi0.png');
% Get part with color
bwimg=img~=0;
% get only the biggest part
lblimg=bwlabel(bwimg,4);
stat = regionprops(lblimg,'Centroid','Area','PixelIdxList');
[maxValue,index] = max([stat.Area]);
todelete=1:size(stat,1);
todelete(index)=[];
for ii=todelete
bwimg(stat(ii).PixelIdxList)=0;
end
%update image with without "noise"
img(~bwimg)=0;
% get the contour of the image (thanks #rayryeng)
er = imerode(bwimg, strel('square', 3));
out = imsubtract(bwimg, er);
% we will increase the boundary so we pick a larger region
% Here you need your input. it depedns how much you dilate the image, the
% part of the of the image that will be considered boudnary will increase.
boundary=imdilate(out,strel('square', 10));
% now lets see withc colors are purple. For that we get HSV space. Shades
% of purple are aruond 265~335 H
hsvc=rgb2hsv(c);
purple=find(hsvc(:,1)>250/360&hsvc(:,1)<335/360);
% Get purple in the whole image
purpleimg=zeros(size(img));
for ii=1:size(purple)
purpleimg(img==purple(ii))=purple(ii);
end
% get locations of purple in the boudnary
purpbound=purpleimg.*boundary~=0;
% delete them from the original image
imgNOpurple=img;
imgNOpurple(purpbound)=0;
% plot results
figure(1)
subplot(221)
imshow(purpleimg,c)
title('purple in the image')
subplot(222)
imshow(purpleimg.*boundary,c);
title('purple boundary')
subplot(223)
imshow(img,c)
title('original image')
subplot(224)
imshow(imgNOpurple,c);
title('Image without purple boundary')

To detect irregular circles in MATLAB

I am new to MATLAB Image Processing. I am writing a code to detect some irregular circles, remove the remaining noise from the Image and find the center mean point of the irregular Black circles (ellipse). Here is the Image
This is the code I have written so far
m = imread('cbnimg.jpg');
imshow(m)
im = mean(m,3);
im = (im-min(im(:))) / (max(im(:))-min(im(:)));
figure;
imshow(im,[]);
impixelinfo
figure;
bin = im2bw(im);
imshow(bin);
figure;
bin = edge(bin);
SE = strel('disk',2);
cir =~imdilate(bin,SE);
imshow(cir);
Here is the result image of this code
[IMG]http://i61.tinypic.com/30n9egn.png[/IMG]
I want to detect only the black spots (Irregular Cicrcle) and remove the remaining noise from the picture as I want the Center Mean Point of these Black irregular Circles..
Can anyone suggest me some algorithms or techniques to get my center mean point?
Thank You
A very naïve approach: apply erosion twice and the dilation twice after a binarization:
m = imread('cbnimg.jpg');
imshow(m)
im = mean(m,3);
im = (im-min(im(:))) / (max(im(:))-min(im(:)));
bin = im2bw(im);
SE = strel('disk',10);
bin = ~imerode(~bin,SE);
bin = ~imerode(~bin,SE);
bin =~imdilate(~bin,SE);
bin =~imdilate(~bin,SE);
imshow(bin);
The shape of the circles is a bit changed, but the change in the center point should be really small. If you want something more ellaborated and robust, erode, label the elements in the image, divide them in two clusters depending on the mass (number of pixels) of each label (with k-means for instance) and then discard all the label correspoding to the cluster with lower masses.
However, for what you asked so far this should be enough.

Matlab - How to manually trace a boundary of an object

I am trying to find and extract the boundary of a binary image in matlab, without using something like bwboundaries.
Is there a more manual way to do this, using loops maybe and changing the colour of the boundary pixels.
Any help would be appreciated.
Since it is binary so you can use two loop pairs.
-first ensure that image is binary.(just in case it is greyscale threshold it)
-use the first loop pair for height and one for width and trace any change i.e if a pixel is black and the next pixel is white draw that point onto a new mat.(i.e mark that point as 255 or of any colour you desire)
-Do the same for width and height and store it into another mat.
-Then add both the mats and average out the result.
This is the manual way and it may not be efficient.But it helps you modify the process to get you the exact edges.
(Source:I had used this technique for detecting perspective transformed rectangles containing bar code in java since canny edge used to give out too many edges due to bar code lines)
I don't understand what you mean by manual way. Is it mean pixel by pixel or anything else? Anyway try this example and fully explain your question that what you really want.
d1 = double(imread('cameraman.TIF'))./255; %# Load the image, scale from 0 to 1
subplot(2,2,1); imshow(d1); title('d1'); %# Plot the original image
d = edge(d1,'canny',.6); %# Perform Canny edge detection
subplot(2,2,2); imshow(d); title('d'); %# Plot the edges
ds = bwareaopen(d,40); %# Remove small edge objects
subplot(2,2,3); imshow(ds); title('ds'); %# Plot the remaining edges
iout = d1;
BW = ds;
iout(:,:,1) = iout; %# Initialize red color plane
iout(:,:,2) = iout(:,:,1); %# Initialize green color plane
iout(:,:,3) = iout(:,:,1); %# Initialize blue color plane
iout(:,:,2) = min(iout(:,:,2) + BW, 1.0); %# Add edges to green color plane
iout(:,:,3) = min(iout(:,:,3) + BW, 1.0); %# Add edges to blue color plane
subplot(2,2,4); imshow(iout); title('iout'); %# Plot the resulting image
you can also track boundary by using Blob method but it depend on your requirements.

MATLAB Image Processing - Find Edge and Area of Image

As a preface: this is my first question - I've tried my best to make it as clear as possible, but I apologise if it doesn't meet the required standards.
As part of a summer project, I am taking time-lapse images of an internal melt figure growing inside a crystal of ice. For each of these images I would like to measure the perimeter of, and area enclosed by the figure formed. Linked below is an example of one of my images:
The method that I'm trying to use is the following:
Load image, crop, and convert to grayscale
Process to reduce noise
Find edge/perimeter
Attempt to join edges
Fill perimeter with white
Measure Area and Perimeter using regionprops
This is the code that I am using:
clear; close all;
% load image and convert to grayscale
tyrgb = imread('TyndallTest.jpg');
ty = rgb2gray(tyrgb);
figure; imshow(ty)
% apply a weiner filter to remove noise.
% N is a measure of the window size for detecting coherent features
N=20;
tywf = wiener2(ty,[N,N]);
tywf = tywf(N:end-N,N:end-N);
% rescale the image adaptively to enhance contrast without enhancing noise
tywfb = adapthisteq(tywf);
% apply a canny edge detection
tyedb = edge(tywfb,'canny');
%join edges
diskEnt1 = strel('disk',8); % radius of 4
tyjoin1 = imclose(tyedb,diskEnt1);
figure; imshow(tyjoin1)
It is at this stage that I am struggling. The edges do not quite join, no matter how much I play around with the morphological structuring element. Perhaps there is a better way to complete the edges? Linked is an example of the figure this code outputs:
The reason that I am trying to join the edges is so that I can fill the perimeter with white pixels and then use regionprops to output the area. I have tried using the imfill command, but cannot seem to fill the outline as there are a large number of dark regions to be filled within the perimeter.
Is there a better way to get the area of one of these melt figures that is more appropriate in this case?
As background research: I can make this method work for a simple image consisting of a black circle on a white background using the below code. However I don't know how edit it to handle more complex images with edges that are less well defined.
clear all
close all
clc
%% Read in RGB image from directory
RGB1 = imread('1.jpg') ;
%% Convert RPG image to grayscale image
I1 = rgb2gray(RGB1) ;
%% Transform Image
%CROP
IC1 = imcrop(I1,[74 43 278 285]);
%BINARY IMAGE
BW1 = im2bw(IC1); %Convert to binary image so the boundary can be traced
%FIND PERIMETER
BWP1 = bwperim(BW1);
%Traces perimeters of objects & colours them white (1).
%Sets all other pixels to black (0)
%Doing the same job as an edge detection algorithm?
%FILL PERIMETER WITH WHITE IN ORDER TO MEASURE AREA AND PERIMETER
BWF1 = imfill(BWP1); %This opens figure and allows you to select the areas to fill with white.
%MEASURE PERIMETER
D1 = regionprops(BWF1, 'area', 'perimeter');
%Returns an array containing the properties area and perimeter.
%D1(1) returns the perimeter of the box and an area value identical to that
%perimeter? The box must be bounded by a perimeter.
%D1(2) returns the perimeter and area of the section filled in BWF1
%% Display Area and Perimeter data
D1(2)
I think you might have room to improve the effect of edge detection in addition to the morphological transformations, for instance the following resulted in what appeared to me a relatively satisfactory perimeter.
tyedb = edge(tywfb,'sobel',0.012);
%join edges
diskEnt1 = strel('disk',7); % radius of 4
tyjoin1 = imclose(tyedb,diskEnt1);
In addition I used bwfill interactively to fill in most of the interior. It should be possible to fill the interior programatically but I did not pursue this.
% interactively fill internal regions
[ny nx] = size(tyjoin1);
figure; imshow(tyjoin1)
tyjoin2=tyjoin1;
titl = sprintf('click on a region to fill\nclick outside window to stop...')
while 1
pts=ginput(1)
tyjoin2 = bwfill(tyjoin2,pts(1,1),pts(1,2),8);
imshow(tyjoin2)
title(titl)
if (pts(1,1)<1 | pts(1,1)>nx | pts(1,2)<1 | pts(1,2)>ny), break, end
end
This was the result I obtained
The "fractal" properties of the perimeter may be of importance to you however. Perhaps you want to retain the folds in your shape.
You might want to consider Active Contours. This will give you a continous boundary of the object rather than patchy edges.
Below are links to
A book:
http://www.amazon.co.uk/Active-Contours-Application-Techniques-Statistics/dp/1447115570/ref=sr_1_fkmr2_1?ie=UTF8&qid=1377248739&sr=8-1-fkmr2&keywords=Active+shape+models+Andrew+Blake%2C+Michael+Isard
A demo:
http://users.ecs.soton.ac.uk/msn/book/new_demo/Snakes/
and some Matlab code on the File Exchange:
http://www.mathworks.co.uk/matlabcentral/fileexchange/28149-snake-active-contour
and a link to a description on how to implement it: http://www.cb.uu.se/~cris/blog/index.php/archives/217
Using the implementation on the File Exchange, you can get something like this:
%% Load the image
% You could use the segmented image obtained previously
% and then apply the snake on that (although I use the original image).
% This will probably make the snake work better and the edges
% in your image is not that well defined.
% Make sure the original and the segmented image
% have the same size. They don't at the moment
I = imread('33kew0g.jpg');
% Convert the image to double data type
I = im2double(I);
% Show the image and select some points with the mouse (at least 4)
% figure, imshow(I); [y,x] = getpts;
% I have pre-selected the coordinates already
x = [ 525.8445 473.3837 413.4284 318.9989 212.5783 140.6320 62.6902 32.7125 55.1957 98.6633 164.6141 217.0749 317.5000 428.4172 494.3680 527.3434 561.8177 545.3300];
y = [ 435.9251 510.8691 570.8244 561.8311 570.8244 554.3367 476.3949 390.9586 311.5179 190.1085 113.6655 91.1823 98.6767 106.1711 142.1443 218.5872 296.5291 375.9698];
% Make an array with the selected coordinates
P=[x(:) y(:)];
%% Start Snake Process
% You probably have to fiddle with the parameters
% a bit more that I have
Options=struct;
Options.Verbose=true;
Options.Iterations=1000;
Options.Delta = 0.02;
Options.Alpha = 0.5;
Options.Beta = 0.2;
figure(1);
[O,J]=Snake2D(I,P,Options);
If the end result is an area/diameter estimate, then why not try to find maximal and minimal shapes that fit in the outline and then use the shapes' area to estimate the total area. For instance, compute a minimal circle around the edge set then a maximal circle inside the edges. Then you could use these to estimate diameter and area of the actual shape.
The advantage is that your bounding shapes can be fit in a way that minimizes error (unbounded edges) while optimizing size either up or down for the inner and outer shape, respectively.

How to remove noise near the edge of an object in an image

I have an image like this:
I would like to remove the background(part A) near the edge of the object. I plan to use color detection since the color of object and noise are a little bit different. But maybe it is not a good idea.
I would appreciate if you could have any idea for me.
Thanks
If you're performing any sort of color-based segmentation of images, you may find it easier to convert to HSV color space first to select specific color ranges. I outline how you can do this sort of thing in an answer I gave to a similar question. The steps you would likely want to follow would be the following:
Convert to HSV and create a binary mask by selecting pixels with hues within a green color range and with a minimum amount of saturation and value.
Erode the resulting mask by a certain amount to remove small spurious clusters.
Dilate the eroded mask to get back to a smoother edge for your selected pixels.
I can't give an exact example of how you would apply this analysis to your data since the image you provide in the question actually has a higher resolution than the data it displays, but here's a general solution that uses the functions RGB2HSV, IMERODE, and IMDILATE (the last two are from the Image Processing Toolbox):
rgbImage = imread('data.jpg'); %# Load the RGB image
hsvImage = rgb2hsv(rgbImage); %# Convert to HSV color space
hPlane = 360.*hsvImage(:,:,1); %# Get the hue plane, scaled from 0 to 360
vPlane = hsvImage(:,:,3); %# Get the value plane
mask = (hPlane >= 80) & (hPlane <= 140) & (vPlane >= 0.3); %# Select a mask
SE = strel('disk',10); %# Create a disk-shaped element
mask = imdilate(imerode(mask,SE),SE); %# Erode and dilate the mask
And here's some code to visualize the edges created by the above analysis:
edgeMask = mask-imerode(mask,strel('disk',1)); %# Create an edge mask
edgeImage = zeros([size(edges) 3]); %# Create an RGB image for the edge
edgeImage(find(edgeMask)) = 1; %# that's colored red
image(rgbImage); %# Plot the original image
hold on;
image(edgeImage,'AlphaData',edgeMask); %# Plot the edge image over it