Undefined function or variable
조회 수: 2 (최근 30일)
이전 댓글 표시
In the Hough Transform section line 49 i get the error Undefined function or variable 'BW'. How can i solve this?
f=figure;
imshow('resim1.jpg');
[x, y] = getpts(f);
line (x(1)*ones(2,1),[-1000,1000],'Color', 'black', 'LineWidth', 6);
line (x(2)*ones(2,1),[-1000,1000],'Color', 'black', 'LineWidth', 6);
F = getframe(gcf);
I = frame2im(F);
I3=rgb2gray(I);
imshow(I3);
%Sobel filtre
% Read Input Image
f = I3
imshow(I3);
% Displaying Input Image
f = uint8(f);
figure, imshow(f); title('Input Image');
% Convert the image to double
f = double(f);
% Pre-allocate the filtered_image matrix with zeros
filtered_image = zeros(size(f));
% Sobel Operator Mask
Mx = [-1 0 1; -2 0 2; -1 0 1];
My = [-1 -2 -1; 0 0 0; 1 2 1];
for i = 1:size(f, 1) - 2
for j = 1:size(f, 2) - 2
% Gradient approximations
Gx = sum(sum(Mx.*f(i:i+2, j:j+2)));
Gy = sum(sum(My.*f(i:i+2, j:j+2)));
% Calculate magnitude of vector
filtered_image(i+1, j+1) = sqrt(Gx.^2 + Gy.^2);
end
end
% Displaying Filtered Image
filtered_image = uint8(filtered_image);
figure, imshow(filtered_image); title('Filtered Image');
%Hough Transform
[H,theta,rho] = hough(BW)
P = houghpeaks(H,35,'threshold',ceil(0.3*max(H(:))));
lines = houghlines(BW,T,R,P,'FillGap', 15, 'MinLength', 5);
figure, imshow(I,[]), hold on
max_len = 0;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth', 2, 'Color', 'green');
plot(xy(1,1), xy(1,2),'x', 'LineWidth', 2,'Color', 'yellow');
plot(xy(2,1), xy(2,2),'x', 'LineWidth', 2,'Color', 'red');
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
[lab,num]=bwlabel(BW);
[r,c]=find(lab==1);
[r1, c1] = find(lab==2);
D = pdist2 ([r c], [r1 c1], 'euclidean');
[r2, c2]=find(D==min(D(:)));
point_1=[r(r2) c(c2)];
point_2=[r1(c2) c1(c2)];
plot([point_1(2) point_2(2)], [point_1(1) point_2(1)], 'r')
hold off
distance=sqrt( (point_1(1)-point_2(1)).^2 + (point_1(2)-point_2(2)).^2)
댓글 수: 0
채택된 답변
yanqi liu
2021년 12월 27일
clc; clear all; close all;
img = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/844815/image.jpeg');
f=figure;
imshow(img);
[x, y] = getpts(f);
line (x(1)*ones(2,1),[-1000,1000],'Color', 'black', 'LineWidth', 6);
line (x(2)*ones(2,1),[-1000,1000],'Color', 'black', 'LineWidth', 6);
F = getframe(gcf);
I = frame2im(F);
I3=rgb2gray(I);
imshow(I3);
%Sobel filtre
% Read Input Image
f = I3;
imshow(I3);
% Displaying Input Image
f = uint8(f);
figure, imshow(f); title('Input Image');
% Convert the image to double
f = double(f);
% Pre-allocate the filtered_image matrix with zeros
filtered_image = zeros(size(f));
% Sobel Operator Mask
Mx = [-1 0 1; -2 0 2; -1 0 1];
My = [-1 -2 -1; 0 0 0; 1 2 1];
for i = 1:size(f, 1) - 2
for j = 1:size(f, 2) - 2
% Gradient approximations
Gx = sum(sum(Mx.*f(i:i+2, j:j+2)));
Gy = sum(sum(My.*f(i:i+2, j:j+2)));
% Calculate magnitude of vector
filtered_image(i+1, j+1) = sqrt(Gx.^2 + Gy.^2);
end
end
% Displaying Filtered Image
filtered_image = uint8(filtered_image);
figure, imshow(filtered_image); title('Filtered Image');
BW = im2bw(filtered_image,graythresh(filtered_image));
%Hough Transform
[H,T,R] = hough(BW);
P = houghpeaks(H,35,'threshold',ceil(0.3*max(H(:))));
lines = houghlines(BW,T,R,P,'FillGap', 15, 'MinLength', 5);
figure, imshow(I,[]), hold on
max_len = 0;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth', 2, 'Color', 'green');
plot(xy(1,1), xy(1,2),'x', 'LineWidth', 2,'Color', 'yellow');
plot(xy(2,1), xy(2,2),'x', 'LineWidth', 2,'Color', 'red');
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
[lab,num]=bwlabel(BW);
[r,c]=find(lab==1);
[r1, c1] = find(lab==2);
D = pdist2 ([r c], [r1 c1], 'euclidean');
[r2, c2]=find(D==min(D(:)));
point_1=[r(r2) c(c2)];
point_2=[r1(c2) c1(c2)];
plot([point_1(2) point_2(2)], [point_1(1) point_2(1)], 'r', 'LineWidth', 2)
hold off
distance=sqrt( (point_1(1)-point_2(1)).^2 + (point_1(2)-point_2(2)).^2)
댓글 수: 0
추가 답변 (1개)
Voss
2021년 12월 26일
This error happens because there is no variable called 'BW' defined. Perhaps it should be:
[H,theta,rho] = hough(filtered_image)
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!