허프만 변환 관련 질문

3 Ansichten (letzte 30 Tage)
GSY
GSY am 15 Nov. 2023
Kommentiert: Angelo Yeo am 16 Nov. 2023
결과가... 이상하게 나옵니다..
<주어진 함수 코드>
function res = hough2(image)
% input parameter is image
edges = edge(image,'canny');
[x,y] = find(edges);
angles=[-90:180]*pi/180;
r=int16(floor(x*cos(angles) + y*sin(angles)));
rmax = max(r(find(r>0)));
acc=zeros(rmax+1,270);
for i=1:length(x)
for j=1:270
if r(i,j) >=0
acc(r(i,j)+1,j) = acc(r(i,j)+1,j) + 1;
end
end
end
res = acc;
end
````````````````````````
function houghline(image,r,theta)
%UNTITLED3 이 함수의 요약 설명 위치
% 자세한 설명 위치
[x,y] = size(image);
angle = pi*(181-theta)/180;
X = [1:x];
if sin(angle) == 0
line([r r], [0,y],'Color','red');
else
line([0,y],[r/sin(angle),(r-y*cos(angle))/sin(angle)],'Color','red');
end
```````````````````````````````````````
<제가 작성한 코드>
%% Hough Transform
normalneck = imread('normalneck.PNG');
normalneck = im2gray(normalneck); % 안하면 차원 오류남.
textneck = imread('textneck.PNG'); % 거북목 증후군
textneck = im2gray(textneck);
% Apply Hough Transform
h1 = hough2(normalneck); figure, imshow(mat2gray(h1)*1.5);title('Hough1');
h2 = hough2(textneck); figure, imshow(mat2gray(h2)*1.5);title('Hough2');
sorted_h1 = sort(h1(:), 'descend');
sorted_h2 = sort(h2(:), 'descend');
figure, imshow(normalneck); hold on;
for i = 1:100
[r, theta] = find(h1 == sorted_h1(i));
houghline(normalneck, r, theta);
end
hold off;
figure, imshow(textneck); hold on;
for i = 1:100
[r, theta] = find(h2 == sorted_h2(i));
houghline(textneck, r, theta);
end
hold off;
``````````````````````````````````````
위의 코드를 실행하면,
-> 다음 사용 중 오류가 발생함: line
벡터들의 길이는 같아야 합니다.
오류 발생: houghline (11번 라인)
line([0,y],[r/sin(angle),(r-y*cos(angle))/sin(angle)],'Color','red');
이런 오류가 발생하면서 정답과는 다른 결과가 나옵니다...
어떻게 해결할 수 있을까요? ㅠㅠㅠ

Akzeptierte Antwort

Angelo Yeo
Angelo Yeo am 16 Nov. 2023
[r, theta] = find(h2 == sorted_h2(i)); 의 결과로 여러 개의 rtheta가 나올 수 있게 때문에 현재 에러가 발생하는 것으로 보입니다. 아래와 같이 코드를 수정해볼 수 있습니다.
%% Hough Transform
% normalneck = imread('normalneck.PNG');
% normalneck = im2gray(normalneck); % 안하면 차원 오류남.
textneck = imread('mypic.png'); % 거북목 증후군
textneck = im2gray(textneck);
% Apply Hough Transform
% h1 = hough2(normalneck); figure, imshow(mat2gray(h1)*1.5);title('Hough1');
h2 = hough2(textneck); % figure, imshow(mat2gray(h2)*1.5);title('Hough2');
sorted_h2 = sort(h2(:), 'descend');
figure, imshow(textneck); hold on;
for i = 1:100
[r, theta] = find(h2 == sorted_h2(i));
% There may be multiple r's and theta's
for i_r = 1:length(r)
houghline(textneck, r(i_r), theta(i_r));
end
end
hold off;
function res = hough2(image)
% input parameter is image
edges = edge(image,'canny');
[x,y] = find(edges);
angles=[-90:180]*pi/180;
r=int16(floor(x*cos(angles) + y*sin(angles)));
rmax = max(r(find(r>0)));
acc=zeros(rmax+1,270);
for i=1:length(x)
for j=1:270
if r(i,j) >=0
acc(r(i,j)+1,j) = acc(r(i,j)+1,j) + 1;
end
end
end
res = acc;
end
function houghline(image,r,theta)
%UNTITLED3 이 함수의 요약 설명 위치
% 자세한 설명 위치
[x,y] = size(image);
angle = pi*(181-theta)/180;
X = [1:x];
if sin(angle) == 0
line([r r], [0,y],'Color','red');
else
line([0,y],[r/sin(angle),(r-y*cos(angle))/sin(angle)],'Color','red');
end
end
  2 Kommentare
GSY
GSY am 16 Nov. 2023
정말 감사합니다! 덕분에 에러가 해결되고 결과가 잘 도출되었습니다:)
Angelo Yeo
Angelo Yeo am 16 Nov. 2023
도움 되었다니 다행입니다 😁 답변 채택해주시면 추후 도움이 필요하신 분들에게도 도움이 될 수 있으니 채택 부탁드립니다.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!