Finding pixel position after imrotate (matlab) - matlab

Hello
I have a problem of finding pixel position after imrotate
`img = imread('rice.png');
point = [100;120];
angle = 45;
img_rot = imrotate(img,angle);
new_point ??`

Simple way: Apply the same transformation to a image with a single dot:
s=size(img);
marker=zeros(s(1:2));
marker(point(1),point(2))=1;
marker_rot = imrotate(marker,angle);
[x,y]=find(marker_rot)

Related

Adjust regionprops orientation to 0-360 degrees (Matlab)

I would like to align video frames to center and reorient an animal walking across the frames. I binarized each frame and fit an ellipse to the animal's body.
Binarized with ellipse fit
I used regionprops to find the ellipse, which outputs orientation from -90 to 90 degrees. I rotate each frame depending on the difference between orientation and the desired alignment orientation. The problem I'm having is that sometimes the orientation is aligned to the head and sometimes to the tail, so the alignment is occasionally 180 degrees flipped.
Upright position
Flipped position
I think this can be resolved by putting the orientation output in 0-360 space. Does anyone have a solution for this?
Here is my code:
for i = 1000
frame = read(v,i);
BW = im2bw(frame,0.95);
imshowpair(frame,BW,'montage')
% Extract the maximum area
BW = imclearborder(BW);
BW = bwareafilt(BW,1);
% Calculate centroid, orientation and major/minor axis length of the ellipse
s = regionprops(BW,{'Centroid','Orientation','MajorAxisLength','MinorAxisLength'});
centroid(i,:) = s.Centroid;
orientation(i) = s.Orientation;
fixed_orientation(i) = s.Orientation;
delta_angle(i) = -90 - orientation(i);
if i >=2
if diff(sign( fixed_orientation(i-1:i) )) == 2 && abs(fixed_orientation(i-1))-abs(fixed_orientation(i))>=20
fixed_orientation(i) = -fixed_orientation(i);
delta_angle(i) = -(-90 - fixed_orientation(i));
end
end
% Calculate the ellipse line
theta = linspace(0,2*pi);
col = (s.MajorAxisLength/2)*cos(theta);
row = (s.MinorAxisLength/2)*sin(theta);
M = makehgtform('translate',[s.Centroid, 0],'zrotate',deg2rad(-1*s.Orientation));
D = M*[col;row;zeros(1,numel(row));ones(1,numel(row))];
% Visualize the result
figure
imshow(BW)
hold on
plot(D(1,:),D(2,:),'r','LineWidth',2)
end
figure
for i = 1:1000
rotate_filler = imrotate(read(v,i), delta_angle(i), 'crop');
rotated_image(:,:,:,i) = rotate_filler;
imshowpair(read(v,i), rotated_image(:,:,:,i), 'montage')
pause(0.001)
end
Any suggestions are appreciated. Thanks!

Finding the pixel displacement in fish eye images

I am trying to plot the displacement of a pixel from the original image to the fish eye image based on the radius from the center of the image.
I was successful in producing fish images in MATLAB using maketform
testImg = imread('ship.jpg');
optTra = maketform('custom',2,2,[],#radial,options);
newX = imtransform(testImg,optTra);
imshow(newX);
the radial function here helps me to get the fish eye distorted image.
I need to find the displacement of each pixel in the original image to that of the distorted image.
If the transformation applied (a.k.a "#radial") was angular, the inverse transformation is given by:
u = r cos(phi) + 0.5;
v = r sin(phi) + 0.5;
where
r = atan2(sqrt(x*x+y*y),p.z)/pi;
phi = atan2(y,x);
x,y are assumed to be normalized coordinates (centered and between -1 to 1).

unable to draw a line correctly using hough transformation in matlab

I used this code for drawing lines using Hough tranform. In this code, there is a draw of the image in Polar space . I want to draw detected lines in Cartesian space. Thus, with the help of this explanation I wrote my code like this:
imBinary = flipud(edge(rgb2gray(im),'sobel'));
[imWidth,imLenght]=size(imBinary);
[interestPointY , interestPointX] = find(imBinary);
numberOfInterestingPoints=numel(interestPointX);
maxRadius=sqrt(imWidth^2 + imLenght^2);
polarRadius=(-maxRadius:1:maxRadius);
polarTheta=(-pi/2:(1/200):pi/2);
numberOfPolarThetas=numel(polarTheta);
houghMatrix=zeros(numel(polarRadius),numberOfPolarThetas);
polarAccumulator = zeros(numberOfInterestingPoints,numberOfPolarThetas);
cosine = (0:imWidth-1)'*cos(polarTheta);
sine = (0:imLenght-1)'*sin(polarTheta);
polarAccumulator((1:numberOfInterestingPoints),:) =cosine(interestPointY,:) + sine(interestPointX,:);
for i = (1:numberOfPolarThetas)
houghMatrix(:,i) = hist(polarAccumulator(:,i),polarRadius);
end
radiusDetect=[];angleDetect=[];
houghBinaryMax = imregionalmax(houghMatrix);
[Potential_radius ,Potential_angle] = find(houghBinaryMax == 1);
%[radiusIndex angleIndex] = find(houghMatrix > 0);
houghTmp= houghMatrix - lineThreshold;
for cnt = 1:numel(Potential_radius)
if houghTmp(Potential_radius(cnt),Potential_angle(cnt)) >= 0
radiusDetect = [radiusDetect;Potential_radius(cnt)];
angleDetect = [angleDetect;Potential_angle(cnt)];
end
end
radius=polarRadius(radiusDetect);
deg=polarTheta(angleDetect);
%find two points in cartesian space
x=radius.*cos(deg);
y=radius.*sin(deg);
x1=x+5.*cos(deg);
y1=y+5.*cos(deg);
imshow(imBinary);
hold on;
plot([x x1],[y y1]);
Unfortunately I did not get the correct lines and my image in like this:the blue lines are detected
My question is what the mistake is that I cannot draw lines correctly?

Traverse a circular path

I have an image that consists of concentric circles. How do I traverse each circle separately (knowing the Center coordinates and radius) in MATLAB?
if I think I understand the question correctly you are looking for a circle around a given point on an image. I have posted some code below, that will retrieve these points for you.
im = zeros([50,50]);
center = [20,20];
radius = 5;
x = 1:size(im,1);
y = 1:size(im,2);
[xx,yy] = meshgrid(x-center(1),y-center(2));
dist = sqrt(xx.^2+yy.^2)
circle = dist > radius-1 & dist < radius+1;
im would just be whatever the image is your are looking at

Stretching an ellipse in an image to form a circle

I want to stretch an elliptical object in an image until it forms a circle. My program currently inputs an image with an elliptical object (eg. coin at an angle), thresholds and binarizes it, isolates the region of interest using edge-detect/bwboundaries(), and performs regionprops() to calculate major/minor axis lengths.
Essentially, I want to use the 'MajorAxisLength' as the diameter and stretch the object on the minor axis to form a circle. Any suggestions on how I should approach this would be greatly appreciated. I have appended some code for your perusal (unfortunately I don't have enough reputation to upload an image, the binarized image looks like a white ellipse on a black background).
EDIT: I'd also like to apply this technique to the gray-scale version of the image, to examine what the stretch looks like.
code snippet:
rgbImage = imread(fullFileName);
redChannel = rgbImage(:, :, 1);
binaryImage = redChannel < 90;
labeledImage = bwlabel(binaryImage);
area_measurements = regionprops(labeledImage,'Area');
allAreas = [area_measurements.Area];
biggestBlobIndex = find(allAreas == max(allAreas));
keeperBlobsImage = ismember(labeledImage, biggestBlobIndex);
measurements = regionprops(keeperBlobsImage,'Area','MajorAxisLength','MinorAxisLength')
You know the diameter of the circle and you know the center is the location where the major and minor axes intersect. Thus, just compute the radius r from the diameter, and for every pixel in your image, check to see if that pixel's Euclidean distance from the cirlce's center is less than r. If so, color the pixel white. Otherwise, leave it alone.
[M,N] = size(redChannel);
new_image = zeros(M,N);
for ii=1:M
for jj=1:N
if( sqrt((jj-center_x)^2 + (ii-center_y)^2) <= radius )
new_image(ii,jj) = 1.0;
end
end
end
This can probably be optimzed by using the meshgrid function combined with logical indices to avoid the loops.
I finally managed to figure out the transform required thanks to a lot of help on the matlab forums. I thought I'd post it here, in case anyone else needed it.
stats = regionprops(keeperBlobsImage, 'MajorAxisLength','MinorAxisLength','Centroid','Orientation');
alpha = pi/180 * stats(1).Orientation;
Q = [cos(alpha), -sin(alpha); sin(alpha), cos(alpha)];
x0 = stats(1).Centroid.';
a = stats(1).MajorAxisLength;
b = stats(1).MinorAxisLength;
S = diag([1, a/b]);
C = Q*S*Q';
d = (eye(2) - C)*x0;
tform = maketform('affine', [C d; 0 0 1]');
Im2 = imtransform(redChannel, tform);
subplot(2, 3, 5);
imshow(Im2);