Calculation of pixel shift
Mostrar comentarios más antiguos
Hi,
I have a slated edge image and I want to know the value of required pixel shift to make it vertical. I know the pixel value at first and end point of the edge. But want to scan the image line by line and know the values of shifted pixels for each line. But I am getting an error: *Matrix dimensions must agree for the following line: dif = abs(bw(i:j)- bw(i:j+1))
close all;
clear all;
clc;
I=imread('C:\Users\swati\OneDrive\Documents\MATLAB\Examples\Cropped Image.jpg');
figure;
imshow(I); title('Original Image');
impixelinfo;
m=167;n=200;
%Averaging The Pixels
h=ones(10,10)/100; %Create a normalized pixel; averaging filter.
I_avg=imfilter(I,h); %apply filter to image
figure(); imshow(I_avg);
title(I_avg);
impixelinfo;
%Scanning of Image
v=I_avg(1,:); %scanning the I_avg Intensity
figure;plot(v);
%converting GrayScale image to Binary Image
for x=1:200
for y=1:m
if I_avg(x,y)>mean(I_avg(:))
bw(x,y)=1;
elseif I_avg(x,y)<mean(I_avg(:))
bw(x,y)=0;
end
end
end
figure; imshow(bw);
impixelinfo;
%finding first left most pixel when intensity drops to zero in first row
fisrt_value=zeros([1,m]);
fist_value=bw(1,:)
%finding first left most pixel when intensity drops to zero in last row
second_value=zeros([1,m]);
last_value=bw(200,:)
p2x=1;
p2y=56;
p1x=200;
p1y=73;
m=(p2y-p1y)/(p2x-p1x) %find the slope
theta=atand(m)
shifted_pixel=p1y-p2y % number of shifted pixels
j=55;
for i = 1:20:200
dif = abs(bw(i:j)- bw(i:j+1))
fprintf('Row Number=%0.0f',i);
pos=find(dif==1)
shift=p1y-pos
j=j+1;
i= i+1;
end
Could anyone please suggest me what is wrong in my code or is there any other way to do that?
Thanks, Swati
Respuesta aceptada
Más respuestas (2)
Walter Roberson
el 30 de Jul. de 2017
In
abs(bw(i:j)- bw(i:j+1))
the left side, bw(i:j) has j-i+1 elements. The right side, bw(i:j+1) has j+1-i+1 = j-i+2 elements.
I suggest
abs(diff(bw(i:j+1)))
9 comentarios
Walter Roberson
el 30 de Jul. de 2017
Note: your code
for x=1:200
for y=1:m
if I_avg(x,y)>mean(I_avg(:))
bw(x,y)=1;
elseif I_avg(x,y)<mean(I_avg(:))
bw(x,y)=0;
end
end
end
might not create bw at all, and if it does then it might not be the size you expect. Consider what happens if I_avg(x,y) == mean(I_avg(:)) -- such as could happen if I_avg was completely constant.
Note that mean(I_avg(:)) does not change in the loops, so there is no point in repeatedly calculating it:
mI = mean(I_avg(:));
for x=1:200
for y=1:m
if I_avg(x,y)>mI
bw(x,y)=1;
elseif I_avg(x,y)<mI
bw(x,y)=0;
end
end
end
And you can replace that all with
mI = mean(I_avg(:));
extract = I_avg(1:200, 1:m);
bw = extract > mI;
bw(extract == mI) = nan; %contents undefined in this case
Swati Jain
el 30 de Jul. de 2017
Walter Roberson
el 30 de Jul. de 2017
bw = double(extract > mI);
Walter Roberson
el 30 de Jul. de 2017
You have
j=55;
for i = 1:20:200
dif = abs(bw(i:j)- bw(i:j+1))
fprintf('Row Number=%0.0f',i);
pos=find(dif==1)
shift=p1y-pos
j=j+1;
i= i+1;
end
so in the first iteration, i = 1, j = 55. 1:55 is valid subscripts so you could potentially get some results from the dif. Then, the j=j+1 increments j to 56. The i = i+1 changes i to 2. Then i gets overwritten with 21 by the next iteration of "for i = 1:20:200" . When you have a "for" loop, any changes you make to the loop control variable will be discarded as soon as the next iteration starts. Think of it has having a hidden "real" loop control variable.
So second iteration, i = 21, j = 56. 21:56 is valid subscripts so you could potentially get some results from the dif. j gets incremented to 57. i gets changed to 22, but that doesn't matter, as explained above.
Third iteration, i = 41, j = 57, 41:57 is valid, you might get something in dif. j becomes 58, i becomes 42 but that doesn't matter.
Fourth iteration, i = 61, j = 58, 61:58 is the empty range, so you cannot get any results from the loop. j becomes 59, i becomes 62 but that doesn't matter.
All subsequent iterations of i also lead to empty vectors because i > j.
Image Analyst
el 31 de Jul. de 2017
But the thing I'm wondering about is that bw starts as a 2-D matrix:
fist_value=bw(1,:)
then suddenly, without any reason or comment at to why, you're indexing bw with one index:
dif = abs(bw(i:j)- bw(i:j+1))
so, you either are using a linear index (unlikely), or you really meant to use a comma instead of a colon:
dif = abs(bw(i, j)- bw(i, j+1))
Are you 100% SURE you want to use a linear index (the first dif) instead of two indexes for row and column (the second dif)?
Swati Jain
el 31 de Jul. de 2017
Swati Jain
el 31 de Jul. de 2017
Image Analyst
el 31 de Jul. de 2017
Do we really need to keep discussing this without an image to even look at? What's the big secret? Show us the image.
Swati Jain
el 31 de Jul. de 2017
Image Analyst
el 30 de Jul. de 2017
0 votos
When you say this: abs(bw(i:j)- bw(i:j+1)), then the first vector is (j-i+1) elements long. The second vector, since you're going one element further along the array is (j+1-i+1) = (j-i+2). The second vector is one element longer so you cannot subtract them element-by-element. Not sure what you're going due to the disappointing lack of comments.
If you know the endpoints of the line, you can get the deltay and deltax and use atan2d() and imrotate() to straighten the edge (align it perfectly vertical or horizontal).
Categorías
Más información sobre Blocked Images en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
