MATLAB Boundingbox around object detected with Eigen Value Algorithm - matlab

I have an image of an object that I want to crop using the Eigen Value Algorithm, everything is fine until I want to draw a Bounding Box around the detected features to use as the area of significance.
original = imread('1.jpg');
img = rgb2gray(original);
corners = detectMinEigenFeatures(img);
figure;
imshow(original); hold on;
plot(corners.selectStrongest(4000));
%st = regionprops( corners.selectStrongest(4000), 'BoundingBox' );
%rect = st.BoundingBox;
crop = imcrop(original,rect);
figure
imshow(crop);
My problem is that the variable corners is (n x 1) and I don't know how that relates to coordinates in my original image.

your output corner is an object for storing corner points, use corner.Location to get an M-by-2 array of [x y] point coordinate.

Related

Image to Polar Transformation

I need to transform a black/white image to polar coordinates. The original image is saved in a matrix. Right now, I'm iterating through each pixel of the original image and I calculate the polar transformation of each pixel like this:
originX = 0;
originY = 0;
for x = 1:columns
for y = 1:rows
r = sqrt((x-originX)^2 + (y-originY)^2);
a = atand((y-originY)/(x-originX));
polarTrans(r, a) = origImage(x,y);
end
end
The problem is, the calculated new positions for the pixels are NOT positive integer values, so I can't simply save them into another matrix polarTrans. Do you have any suggestions here? How else should I save the transformed image if not in a matrix?
Vectorize, my friend.
% compute all [x,y] pairs for the whole image
[x,y]=meshgrid(1:columns -Xorigin,1:rows -Yorigin);
% Ta-da!
[alpha,rho]=cart2pol(x,y)
Now pixel [i,j] is [x(i,j), y(i,j)] in cartesian coordinates and [rho(i,j),alpha(i,j)] in polar coordinates. You have no need of storing originImage in any other way. Whenever you want to know the polar coordinates of an specific pixel value, you just do [rho(i,j),alpha(i,j)], for originImage(i,j) pixel value.

Find maximum intensity point from center in all directions in matlab matrix

I have images (matrix) in matlab and I look for the maximum intensity point from the center of the matrix in every direction to obtain edges. (I use the gradient of the image and I'm looking for a quadrilateral).
For M (n,m)
My first try was to consider one vector M(1:n/2, m/2), look for the maximum and rotate the image to find all maximums in all other directions.
But : the imrotate function causes many errors (crop or loose) and the reconstructed image doesn't correspond to the shape of the original one.
I tried also to consider vectors directly in the original image from center to all points in the perimeter... but it's not easy!
Do you have an idea to solve this ? Any subtlety in Matlab I don't know?
Thanks guy;
My actual code is
s_im = size(ima, 2)/2;
ima_max = zeros(size(ima));
ima_new = zeros(size(ima));
for a=0:359
im_r = imrotate(ima, a, 'crop');
c= floor(size(im_r,1)/2);
vect_h1 = im_r(c, 1:c);
l = length(vect_h1);
[~, id_h1] = max(vect_h1(:));
[x,y] = rotatePoint([id_h1, c], [c,c], deg2rad(a-180));
ima_max(floor(y), floor(x))= 1;
ima_new(floor(y), floor(x)) = 1;
An error is also that the center computed is not the same in all images...
I suppose you can use improfile to get the intensity along rays emitting from the center pixel:
sz = size(ima);
X = sz(2);
Y = sz(1);
all_end_points = cat(1, [ones(1,Y); 1:Y]', ...
[1:X; Y*ones(1,X)]', ...
[X*ones(1,Y); Y:-1:1]', ...
[X:-1:1; ones(1,X)]' );
cent = repmat( [X/2 Y/2], [size(all_end_points,1), 1]);
all_profs = improfile(ima, all_end_points(:,1), all_end_points(:,2));
Now you have all the profiles from the center, you can look for the max intensity along each.

Matlab: Crop objects from binary image

I'm new to matlab, I have a image and i want to crop all the three circles and store them. My code works for single circle in an image. But fails to work when i have more circles in image.
My code:
im=imread('D:\capture.png');
im_gray = rgb2gray(im);
BW = im2bw(im_gray, graythresh(im));
se = strel('disk',3);
bw2=imopen(BW,se);
bw2=~bw2;
s = regionprops(bw2, 'BoundingBox');
rectangle('Position', s.BoundingBox);
imCrop = imcrop(bw2, s.BoundingBox);
figure, imshow(imCrop);
Any ideas about it?
You almost have it working. Bear in mind that when you do s.BoundingBox by itself, you are only extracting the first circle. As such, I would recommend you make a cell array that stores the individual circles for each bounding box, then run a for loop through all of your bounding boxes. As such, each element in your cell array would be a cropped circle. As such, try doing this:
%// Your code
im=imread('D:\capture.png');
im_gray = rgb2gray(im);
BW = im2bw(im_gray, graythresh(im));
se = strel('disk',3);
bw2=imopen(BW,se);
bw2=~bw2;
s = regionprops(bw2, 'BoundingBox');
%// New code here
circles = cell(1,numel(s));
for idx = 1 : numel(s)
rect = s(idx).BoundingBox;
circles{idx} = imcrop(bw2, rect);
end
circles will now be a cell array of cropped circles. To access the ith circle, simply do:
imCrop = circles{i};
Edit
From your comments, you want to detect the largest and smallest circles. This can easily be done by checking the Area attribute from regionprops. You would find the bounding box that generates the minimum and maximum areas. You would need to modify your regionprops call to include the Area flag. As such:
s = regionprops(bw2, 'BoundingBox', 'Area');
[~,indMin] = min([s.Area]);
[~,indMax] = max([s.Area]);
circleSmall = circles{indMin};
circleLarge = circles{indMax};
The above code will find the circles with the minimum and maximum area, then extract those corresponding circles, assuming you've run the code to extract all of those circles in that for loop I wrote earlier. Bear in mind that I had to enclose s.Area in square braces. The reason why is because when you do this, you'll be able to extract all of the areas as a single array instead of a matrix with singleton dimensions, and min/max can't work on something like that.

How to plot a surface with a texture map

I want to plot a surface with a texture map on it, but the conditions are not the "ideal" ones.
first lets explain what I have.
I have a set of points (~7000) that are image coordinates, in a grid. This points do NOT define perfect squares. IT IS NOT A MESHGRID. For the sake of the question, lets assume that we have 9 points. Lets ilustrate what we have with an image:
X=[310,270,330,430,410,400,480,500,520]
Y=[300,400,500,300,400,500,300,400,500]
Lets say we can get the "structure" of the grid, so
size1=3;
size2=3;
points=zeros(size1,size2,2)
X=[310,270,330;
430,410,400;
480,500,520]
Y=[300,400,500;
300,400,500;
300,400,500]
points(:,:,1)=X;
points(:,:,2)=Y;
And now lets say we have a 3rd dimension, Z.
EDIT: Forgot to add a piece if info. I triangulate the points in the image and get a 3D correspondence, so when displayed in a surface they don't have the X and Y coords of the image, for a simplification of the given data lets say X=X/2 Y=Y/3
And we have:
points=zeros(size1,size2,3)
Z=[300,330,340;
300,310,330;
290,300,300]
surf(points(:,:,1)/2,points(:,:,2)/3,points(:,:,3))
What I want is to plot the surface in 3D with the image texture. Each element should have the texture piece that have in the first image.
This needs to work for huge datasheets. I don't specially need it to be fast.
related post (but I has a meshgrid as initial set of points) : Texture map for a 2D grid
PD: I can post original images + real data if needed, just posted this because i think it is easier with small data.
You can use the texturemap property of surf which works with rectangular meshes as well as with non-rectangular ones.
Creating non-rectangular data points
% creating non-rectangular data points
[X, Y] = meshgrid(1:100, 1:100);
X = X+rand(size(X))*5;
Y = Y+rand(size(X))*5;
which results in the following data points:
Generating height data:
Z = sin(X/max(X(:))*2*pi).*sin(Y/max(Y(:))*2*pi);
Loading picture:
[imageTest]=imread('peppers.png');
and mapping it as texture to the mesh:
surf(X,Y,Z, imageTest, ...
'edgecolor', 'none','FaceColor','texturemap')
Note that, for the sake of demonstration, this non-rectangular grid is quite sparsely populated which results in a rather jagged texture. With more points, the result gets much better, irrespective of the distortion of the grid points.
Note also that the number of grid points does not have to match the number of pixels in the texture image.
~edit~
If X and Y coordinates are only available for parts of the image, you can adjust the texture accordingly by
minX = round(min(X(:)));
maxX = round(max(X(:)));
minY = round(min(Y(:)));
maxY = round(max(Y(:)));
surf(X,Y,Z, imageTest(minX:maxX, minY:maxY, :), ...
'edgecolor', 'none','FaceColor','texturemap')
I don't think you can do what you want with Matlab's built in commands and features. But using the technique from my other answer with a high-res version of the grid can do it for you.
By "high-res", I mean an interpolated version of the non-uniform grid with denser data points. That is used to sample the texture at denser data points so it can be drawn using the texturemap feature of surf. You can't use a normal 2D interpolation, however, because you need to preserve the non-uniform grid shape. This is what I came up with:
function g = nonUniformGridInterp2(g, sx, sy)
[a,b] = size(g);
g = interp1(linspace(0,1,a), g, linspace(0,1,sy)); % interp columns
g = interp1(linspace(0,1,b), g', linspace(0,1,sx))'; % interp rows
Note that you have to call this twice to interpolate the X and Y points independently. Here's an example of the original grid and an interpolated version with 10 points in each direction.
Here's how to use that high-res grid with interp2 and texturemap.
function nonUniformTextureMap
% define the non-uniform surface grid
X = [310,270,330; 430,410,400; 480,500,520];
Y = [300,400,500; 300,400,500; 300,400,500];
Z = [300,330,340; 300,310,330; 290,300,300];
% get texture data
load penny % loads data in variable P
% define texture grid based on image size
% note: using 250-550 so that a,b covers the range used by X,Y
[m,n] = size(P);
[a,b] = meshgrid(linspace(250,550,n), linspace(250,550,m));
% get a high-res version of the non-uniform grid
s = 200; % number of samples in each direction
X2 = nonUniformGridInterp2(X, s, s);
Y2 = nonUniformGridInterp2(Y, s, s);
% sample (map) the texture on the non-uniform grid
C = interp2(a, b, P, X2, Y2);
% plot the original and high-res grid
figure
plot(X(:),Y(:),'o',X2(:),Y2(:),'.')
legend('original','high-res')
% plot the surface using sampled points for color
figure
surf(X, Y, Z, C, 'edgecolor', 'none', 'FaceColor','texturemap')
colormap gray
I'm not sure I understand your question, but I think that what you need to do is sample (map) the texture at your grid's X,Y points. Then you can simply plot the surface and use those samples as colors.
Here's an example using the data you gave in your question. It doesn't look like much, but using more X,Y,Z points should give the result you're after.
% define the non-uniform surface grid
X = [310,270,330; 430,410,400; 480,500,520];
Y = [300,400,500; 300,400,500; 300,400,500];
Z = [300,330,340; 300,310,330; 290,300,300];
% get texture data
load penny % loads data in variable P
% define texture grid based on image size
% note: using 600 so that a,b covers the range used by X,Y
[m,n] = size(P);
[a,b] = meshgrid(linspace(0,600,n), linspace(0,600,m));
% sample (map) the texture on the non-uniform grid
C = interp2(a, b, P, X, Y);
% plot the surface using sampled points for color
figure
surf(X, Y, Z, C)
colormap gray

remove the holes in an image by average values of surrounding pixels

can any one please help me in filling these black holes by values taken from neighboring non-zero pixels.
thanks
One nice way to do this is to is to solve the linear heat equation. What you do is fix the "temperature" (intensity) of the pixels in the good area and let the heat flow into the bad pixels. A passable, but somewhat slow, was to do this is repeatedly average the image then set the good pixels back to their original value with newImage(~badPixels) = myData(~badPixels);.
I do the following steps:
Find the bad pixels where the image is zero, then dilate to be sure we get everything
Apply a big blur to get us started faster
Average the image, then set the good pixels back to their original
Repeat step 3
Display
You could repeat averaging until the image stops changing, and you could use a smaller averaging kernel for higher precision---but this gives good results:
The code is as follows:
numIterations = 30;
avgPrecisionSize = 16; % smaller is better, but takes longer
% Read in the image grayscale:
originalImage = double(rgb2gray(imread('c:\temp\testimage.jpg')));
% get the bad pixels where = 0 and dilate to make sure they get everything:
badPixels = (originalImage == 0);
badPixels = imdilate(badPixels, ones(12));
%# Create a big gaussian and an averaging kernel to use:
G = fspecial('gaussian',[1 1]*100,50);
H = fspecial('average', [1,1]*avgPrecisionSize);
%# User a big filter to get started:
newImage = imfilter(originalImage,G,'same');
newImage(~badPixels) = originalImage(~badPixels);
% Now average to
for count = 1:numIterations
newImage = imfilter(newImage, H, 'same');
newImage(~badPixels) = originalImage(~badPixels);
end
%% Plot the results
figure(123);
clf;
% Display the mask:
subplot(1,2,1);
imagesc(badPixels);
axis image
title('Region Of the Bad Pixels');
% Display the result:
subplot(1,2,2);
imagesc(newImage);
axis image
set(gca,'clim', [0 255])
title('Infilled Image');
colormap gray
But you can get a similar solution using roifill from the image processing toolbox like so:
newImage2 = roifill(originalImage, badPixels);
figure(44);
clf;
imagesc(newImage2);
colormap gray
notice I'm using the same badPixels defined from before.
There is a file on Matlab file exchange, - inpaint_nans that does exactly what you want. The author explains why and in which cases it is better than Delaunay triangulation.
To fill one black area, do the following:
1) Identify a sub-region containing the black area, the smaller the better. The best case is just the boundary points of the black hole.
2) Create a Delaunay triangulation of the non-black points in inside the sub-region by:
tri = DelaunayTri(x,y); %# x, y (column vectors) are coordinates of the non-black points.
3) Determine the black points in which Delaunay triangle by:
[t, bc] = pointLocation(tri, [x_b, y_b]); %# x_b, y_b (column vectors) are coordinates of the black points
tri = tri(t,:);
4) Interpolate:
v_b = sum(v(tri).*bc,2); %# v contains the pixel values at the non-black points, and v_b are the interpolated values at the black points.