Outlining a defect in an image? - image

I have the following image on which i have applied bilateral filtering and subtracted it from my original image
Is it possible to outline the glass defect as shown
After apply Hough i got the following result not perfect :/
My matlab code :
im = imread('C:\Users\SUJIT\Desktop\image003.jpg');
im=rgb2gray(im);
h = fspecial('gaussian', size(im), 1.0);
g = imfilter(im, h);
im=im2double(g);
im=imadjust(im);
imgauss = imfilter(im, fspecial('gaussian',[7 7], 6),'conv');
imbi = bilateralfilter(im, [5 5], 3, 3);
imbi= im - imbi;
imshow(imbi,[]); title('Bilateral Filted Image');
I = imcrop(imbi, [30 30 450 350]);
J = imfilter(I, fspecial('gaussian', [17 17], 5), 'symmetric');
BW = edge(J, 'canny');
%# Hough Transform and show matrix
[H T R] = hough(BW);
imshow(imadjust(mat2gray(H)), [], 'XData',T, 'YData',R, ...
'InitialMagnification','fit')
xlabel('\theta (degrees)'), ylabel('\rho')
axis on, axis normal, hold on
colormap(hot), colorbar
%# detect peaks
P = houghpeaks(H, 10);
plot(T(P(:,2)), R(P(:,1)), 'gs', 'LineWidth',2);
%# detect lines and overlay on top of image
lines = houghlines(BW, T, R, P);
figure, imshow(I), hold on
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1), xy(:,2), 'g.-', 'LineWidth',2);
end
hold off
Please help am i doing something wrong here?

It is a bit hard to give a general answer based on only one image and no other information, but I can give a specific answer based on your sample image.
Assuming that what you want to find is the vertical blurry line in the middle of the image, this my approach. I won't go into specific implementation details, but only an outline on how I would do it.
Find the windows. There are multiple approaches to this. Some ideas are to either find the corners, or to find the rectangular structure itself. The Hough transform is a possible tool.
For each window, check if there are vertical structures in it.

Related

Matlab detect rectangle from image [duplicate]

I need to know how to align an image in Matlab for further work.
for example I have the next license plate image and I want to recognize all
the digits.
my program works for straight images so, I need to align the image and then
preform the optical recognition system.
The method should be as much as universal that fits for all kinds of plates and in all kinds of angles.
EDIT: I tried to do this with Hough Transform but I didn't Succeed. anybody can help me do to this?
any help will be greatly appreciated.
The solution was first hinted at by #AruniRC in the comments, then implemented by #belisarius in Mathematica. The following is my interpretation in MATLAB.
The idea is basically the same: detect edges using Canny method, find prominent lines using Hough Transform, compute line angles, finally perform a Shearing Transform to align the image.
%# read and crop image
I = imread('http://i.stack.imgur.com/CJHaA.png');
I = I(:,1:end-3,:); %# remove small white band on the side
%# egde detection
BW = edge(rgb2gray(I), 'canny');
%# hough transform
[H T R] = hough(BW);
P = houghpeaks(H, 4, 'threshold',ceil(0.75*max(H(:))));
lines = houghlines(BW, T, R, P);
%# shearing transforma
slopes = vertcat(lines.point2) - vertcat(lines.point1);
slopes = slopes(:,2) ./ slopes(:,1);
TFORM = maketform('affine', [1 -slopes(1) 0 ; 0 1 0 ; 0 0 1]);
II = imtransform(I, TFORM);
Now lets see the results
%# show edges
figure, imshow(BW)
%# show accumlation matrix and peaks
figure, imshow(imadjust(mat2gray(H)), [], 'XData',T, 'YData',R, 'InitialMagnification','fit')
xlabel('\theta (degrees)'), ylabel('\rho'), colormap(hot), colorbar
hold on, plot(T(P(:,2)), R(P(:,1)), 'gs', 'LineWidth',2), hold off
axis on, axis normal
%# show image with lines overlayed, and the aligned/rotated image
figure
subplot(121), imshow(I), hold on
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1), xy(:,2), 'g.-', 'LineWidth',2);
end, hold off
subplot(122), imshow(II)
In Mathematica, using Edge Detection and Hough Transform:
If you are using some kind of machine learning toolbox for text recognition, try to learn from ALL plates - not only aligned ones. Recognition results should be equally well if you transform the plate or dont, since by transforming, no new informations according to the true number will enhance the image.
If all the images have a dark background like that one, you could binarize the image, fit lines to the top or bottom of the bright area and calculate an affine projection matrix from the line gradient.

unwrap picture of a half cylinder in MATLAB

My problem is as follows:
I have the picture of a half cylinder taken from a horizontal perspective and it has square grid lines on it, so I was wondering how can I implement in MATLAB to unwrap this half cylinder so all my grid cells become the same size? I know I will loose lots of resolution in the edge cells and a simple linear interpolation should do the trick, but I do not know how to tell MATLAB to do this. Also I know the geometrical properties of the cylinder, radius and height. Any help is greatly appreciated.
This is the approach I am using, but I am trying to find the transformation that will make the edges be same size as inner cells.
im=imread('Capture.png');
imshow(im);
impixelinfo
r = #(x) sqrt(x(:,1).^2 + x(:,2).^2);
w = #(x) atan2(x(:,2), x(:,1));
f = #(x) [sqrt(r(x)) .* cos(w(x)), sqrt(r(x)) .* sin(w(x))];
g = #(x, unused) f(x);
tform2 = maketform('custom', 2, 2, [], g, []);
im3 = imtransform(im, tform2, 'UData', [-1 1], 'VData', [-1 1], ...
'XData', [-1 1], 'YData', [-1 1]);
figure,
imshow(im3)
I think the transformation is much simpler than what you're trying to do. Take a look at the (forward) transformation to take a flat grid and wrap it around a cylinder. The coordinates along the axis of the cylinder (the y coordinates, in this case) are unchanged. If we take the range of the grid coordinates in the x direction to be [-1,1], the coordinates on the cylinder will be:
sin(x × π/2)
Since this is the forward transformation going from a grid to the cylinder, it is also the inverse transformation going from the cylinder to the grid.
f = #(x, unused) [sin(x (:, 1) * pi / 2), x(:, 2)]
tform2 = maketform('custom', 2, 2, [], f, []);
im3=imtransform(img, tform2, 'UData', [-1 1], 'VData', [-1 1], ...
'XData', [-1 1], 'YData', [-1 1]);
Result:
This still isn't perfect, primarily because the original image has borders around it that we're transforming along with the rest of the image. This could be improved by cropping the image to contain only the cylinder portion.

Check if second image is subimage of first image

I want to find out if a given image is an exact or similar part of another image in matlab.
For example, detecting a score bar in a cricket video frame. I would like to detect if there is a scorebar displayed in the given image or not.
1. Larger image
2. Another image
3. Check if this is a subimage
I want to check if 3 is a part of 1 or not. Not an exact part. For example, even if a scorebar exists in 1, and they are not the same scorebars, that would do.
What I am trying:
I am trying to divide the larger image into small parts and take the last part of the image and calculate hue histogram difference with the scorebar image. If it falls below a certain threshold, I should classify that as a part of the bigger image. Is this the right approach or should I follow some other better approach. Please suggest me if you have a better one.
Code I wrote:
rgbImage = imread('img7517.jpg'); %bigger image
[r, c, x] = size(rgbImage);
numberOfBins = 256;
r1 = 6*r/7;
im = rgbImage(r1:r,:,1);
subplot(2,2,1);
imshow(im);
hsv = rgb2hsv(im);
h = hsv(:,:,1);
subplot(2,2,2);
hist(h(:), numberOfBins);
[counts, y] = hist(h(:), numberOfBins);
im1 = imread('scorebar.jpg'); %smaller image
subplot(2,2,3);
imshow(im1);
hsv = rgb2hsv(rgbImage);
h = hsv(:,:,1);
subplot(2,2,4);
hist(h(:), numberOfBins);
[count, y] = hist(h(:), numberOfBins);
c = sum(abs(counts(:) - count(:)));
disp(c);
Problem
But this doesn't give me any significance histogram difference between 1,3 and 2,3. Value of c for 1,3 is 72949 and for 2,3 is 72875. How do I do this? Is the problem due to code or approach? Please help me solve this problem.
Edit:
Trying normalized cross-correlation,
im1 = rgb2gray(imread('replay.jpg'));
im2 = rgb2gray(imread('scorebar1.jpg'));
c = normxcorr2(im2, im1);
[ypeak, xpeak] = find(c==max(c(:)));
yoffSet = ypeak-size(im1,1);
xoffSet = xpeak-size(im1,2);
hFig = figure;
hAx = axes;
imshow(im2,'Parent', hAx);
imrect(hAx, [xoffSet, yoffSet, size(im1,2), size(im1,1)]);
following this link. But doesn't gives a similar analysis.
This class of problem (finding a target image within a larger image) is known as template matching. Typically you might use normalised cross-correlation, but there are various different algorithms, depending on your requirements and specific use case.
Unfortunately your home-brew histogram-based algorithm is probably not going to give very good results, as you have already observed, so you'll probably need to try one of the commonly-used methods described in the articles linked to above.
Solution, I got,
im1 = rgb2gray(imread('img1.jpg'));
im2 = rgb2gray(imread('scorebar.jpg'));
[r, c, x] = size(rgbImage);
numberOfBins = 256;
r1 = 6*r/7;
im1 = im1(r1:r,:,1);
[counts, y] = imhist(im1, numberOfBins);
[count, y] = imhist(im2, numberOfBins);
c = sum(abs(counts(:) - count(:)));
disp(c);
This gives significance hue histogram difference(HHD) between histograms. The images who have scorebars have HHD from 2000-5000, those who don't have scorebars have HHD > 10000.

How to remove non-barcode region in an image? - MATLAB

After I did a 'imclearborder', there are still a bit of unwanted object around the barcode. How can I remove those objects to isolate the barcode? I have pasted my code for your reference.
rgb = imread('barcode2.jpg');
% Resize Image
rgb = imresize(rgb,0.33);
figure(),imshow(rgb);
% Convert from RGB to Gray
Igray = double(rgb2gray(rgb));
% Calculate the Gradients
[dIx, dIy] = gradient(Igray);
B = abs(dIx) - abs(dIy);
% Low-Pass Filtering
H = fspecial('gaussian', 20, 10);
C = imfilter(B, H);
C = imclearborder(C);
figure(),imagesc(C);colorbar;
Well, i have already explained it in your previous question How to find the location of red region in an image using MATLAB? , but with a opencv code and output images.
Instead of asking for code, try to implement it yourself.
Below is what to do next.
1) convert image 'C' in your code to binary.
2) Apply some erosion to remove small noises.( this time, barcode region also shrinks)
3) Apply dilation to compensate previous erosion.(most of noise will have removed in previous erosion. So they won't come back)
4) Find contours in the image.
5) Find their area. Most probably, contour which has maximum area will be the barcode, because other things like letters, words etc will be small ( you can understand it in the grayscale image you have provided)
6) Select contour with max. area. Draw a bounding rectangle for it.
Its result is already provided in your previous question. It works very nice. Try to implement it yourself with help of MATLAB documentation. Come here only when you get an error which you don't understand.
%%hi, i am ading my code to yours at the end of your code%%%%
clear all;
rgb = imread('barcode.jpeg');
% Resize Image
rgb = imresize(rgb,0.33);
figure(),imshow(rgb);
% Convert from RGB to Gray
Igray = double(rgb2gray(rgb));
Igrayc = Igray;
% Calculate the Gradients
[dIx, dIy] = gradient(Igray);
B = abs(dIx) - abs(dIy);
% Low-Pass Filtering
H = fspecial('gaussian', 10, 5);
C = imfilter(B, H);
C = imclearborder(C);
imshow(Igray,[]);
figure(),imagesc(C);colorbar;
%%%%%%%%%%%%%%%%%%%%%%%%from here my code starts%%%%%%%%%%%%%%%%
bw = im2bw(C);%%%binarising the image
% imshow(bw);
%%%%if there are letters or any other noise is present around the barcode
%%Note: the size of the noise and letters should be smaller than the
%%barcode size
labelImage = bwlabel(bw,8);
len=0;labe=0;
for i=1:max(max(labelImage))
a = find(labelImage==i);
if(len<length(a))
len=length(a);
labe=i;
end
end
imag = zeros(size(l));
imag(find(labelImage==labe))=255;
% imtool(imag);
%%%if Necessary do errossion
% se2 = strel('line',10,0);
% imag= imerode(imag,se2);
% imag= imerode(imag,se2);
[r c]= find(imag==255);
minr = min(r);
maxc = max(c);
minc = min(c);
maxr = max(r);
imag1 = zeros(size(l));
for i=minr:maxr
for j=minc:maxc
imag1(i,j)=255;
end
end
% figure,imtool(imag1);
varit = find(imag1==0);
Igrayc(varit)=0;
%%%%%result image having only barcode
imshow(Igrayc,[]);
%%%%%original image
figure(),imshow(Igray,[]);
Hope it is useful

Coloring a matrix in matlab [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I visualize a matrix with colors and values displayed?
I basically have a NxN (edit:N can be up to 80) matrix of double in MATLAB and I when to plot it as an array (I want to see the numbers) and some of the cells should be colored (the function to decide how I color my numbers is independent from the numbers).
I have thought of different ways to do that :
create an grid as an image and overlay it with the text but the plot of MATLAB is going to be horrible as it will remove some pixels to resize the image (my matrix can be around 80x80).
Export to excel ? Don't know how I would color the cells though.
Any help ?
As an image is sometimes more helpful :
Consider the following code:
%# matrix
M = rand(11,11);
[r c] = size(M);
%# text location and labels
[xloc yloc] = meshgrid(1:c,1:r);
xloc = xloc(:); yloc = yloc(:);
str = strtrim(cellstr( num2str(M(:),'%.3g') ));
xticklabels = cellstr( num2str((1:c)','M%d') );
yticklabels = cellstr( num2str((1:r)','M%d') );
%# plot colored cells
mask = M>0.9; %# or any other mask
h = imagesc(1:c, 1:r, ones(size(M)));
set(h, 'AlphaData',mask)
colormap(summer) %# colormap([0 1 0])
set(gca, 'Box','on', 'XAxisLocation','top', 'YDir','reverse', ...
'XLim',[0 c]+0.5, 'YLim',[0 r]+0.5, 'TickLength',[0 0], ...
'XTick',1:c, 'YTick',1:r, ...
'XTickLabel',xticklabels, 'YTickLabel',yticklabels, ...
'LineWidth',2, 'Color','none', ...
'FontWeight','bold', 'FontSize',8, 'DataAspectRatio',[1 1 1]);
%# plot grid
xv1 = repmat((2:c)-0.5, [2 1]); xv1(end+1,:) = NaN;
xv2 = repmat([0.5;c+0.5;NaN], [1 r-1]);
yv1 = repmat([0.5;r+0.5;NaN], [1 c-1]);
yv2 = repmat((2:r)-0.5, [2 1]); yv2(end+1,:) = NaN;
line([xv1(:);xv2(:)], [yv1(:);yv2(:)], 'Color','k', 'HandleVisibility','off')
%# plot text
text(xloc, yloc, str, 'FontSize',8, 'HorizontalAlignment','center');
As you increase the size of the matrix, the text will eventually overlap...
EDIT
Here is a way that works with larger matrices: we use an invisible figure, resize it to something big enough, and use a small font size (I used 2):
M = rand(80,80);
figure('visible','off')
set(gcf, 'Units','Pixels', 'Position', [0, 0, 10000, 10000], ...
'PaperPositionMode','Auto');
set(gca, 'units','normalized', 'position',[0.05 0.02 0.9 0.95])
%# ...
Then at the end, export to file with a high resolution:
%# ...
print -dpng -r600 file.png
You can see the output file here (10025x5962 image, 645KB)
If you are willing to output to html (typically using matlab's publish ability), you can modify one of many html table functions on the file exchange to suit your needs. Here is one example. This will enable you to output text as well as colors.
http://www.mathworks.com/products/matlab/demos.html?file=/products/demos/shipping/matlab/imagedemo.html ?
Then you can try to write text on it
In Excel you can always use conditional formatting, no problems there.
As far as matlab goes, I've never done something like it (although the idea has crossed my mind). Did you have in mind something like this How do I visualize a matrix with colors and values displayed?

Resources