Unable to crack this image enhancement code -- Matlab is throwing errors

  • Thread starter Thread starter Saikiran
  • Start date Start date
  • Tags Tags
    Code Image
AI Thread Summary
The discussion focuses on a MATLAB script that processes an image by converting it to grayscale, allowing user input for selecting points, and then performing pixel-wise operations to create matrices for selected, maximum, and minimum neighboring pixels. The script utilizes the ginput function to capture four points from the image, which are then used to draw lines connecting these points. It iterates over a specified pixel range to populate matrices for selected pixels, maximum neighboring pixels, and minimum neighboring pixels based on comparisons with adjacent pixel values. The script also includes a thresholding step, where it converts the image to binary based on the mean pixel value. Finally, it displays the original, selected, maximum, minimum, and thresholded images in a subplot format. Users are encouraged to share any errors encountered for troubleshooting assistance, emphasizing that MATLAB's handling of indentation differs from Python's.
Saikiran
Messages
1
Reaction score
0
TL;DR Summary
Hi everyone !
The code which I mentioned is the code of image enhancement which I am trying to work on. But, it is throwing errors regarding image size mismatch while adding images and also throwing some indentation errors. Any help regarding this would be greatly apprexiated.
Matlab:
img= imread('Image.bmp');
img= rgb2gray(img);
imshow(img);
pt = ginput(4); % the function ginput is used to get the
input from mouse
x1 = pt(1,1);
y1 = pt(1,2);
x2 = pt(2,1);
y2 = pt(2,2);
x = [x1 x2];
y = [y1 y2];
line(x,y);
x3 = pt(3,1);
y3 = pt(3,2);
x4 = pt(4,1);
y4 = pt(4,2);
x = [x2 x3];
y = [y2 y3];
line(x,y);
x = [x3 x4];
y = [y3 y4];
line(x,y);
x = [x4 x1];
y = [y4 y1];
line(x,y);
sidx=1; % selected original image
sidy=1;
uidx =1; % U image i.e. max neighbouring pixel
uidy =1;
vidx =1; % V image i.e. min neighbouring pixel
vidy =1;
 for idx = bx1:bx3
 for idy = by1:by3
 
 % Selected image matrix is created for selected area pixel
by pixel
 S(sidx,sidy) = img(idx,idy);
 sidy = sidy+1;
 
 % Max. Neighbouring Pixel image matrix is created for
selected area pixel by pixel
 
U(uidx,uidy) = img(idx,idy); %
Centre
 %Checking for max. neighbouring pixel
 if(img(idx-1,idy-1) > img(idx,idy)) % 1
 U(uidx,uidy) = img(idx-1,idy-1);
 end
 if(img(idx-1,idy) > img(idx,idy)) % 2
 U(uidx,uidy) = img(idx-1,idy);
 end
 if(img(idx-1,idy+1) > img(idx,idy)) % 3
 U(uidx,uidy) = img(idx-1,idy+1);
 end
 if(img(idx,idy+1) > img(idx,idy)) % 4
 U(uidx,uidy) = img(idx,idy+1);
 end
 if(img(idx+1,idy+1) > img(idx,idy)) % 5
 U(uidx,uidy) = img(idx+1,idy+1);
end
 if(img(idx+1,idy) > img(idx,idy)) % 6
 U(uidx,uidy) = img(idx+1,idy);
end
 if(img(idx+1,idy-1) > img(idx,idy)) % 7
 U(uidx,uidy) = img(idx+1,idy-1);
 end
 end
uidy = uidy+1;
 
 % Min. Neighbouring Pixel image matrix is created for
selected area pixel by pixel
 V(vidx,vidy) = img(idx,idy); %
Centre
 %Checking for min. neighbouring pixel
 if(img(idx-1,idy-1) < img(idx,idy)) % 1
 V(vidx,vidy) = img(idx-1,idy-1);
 end
 if(img(idx-1,idy) < img(idx,idy)) % 2
 V(vidx,vidy) = img(idx-1,idy);
 end
 if(img(idx-1,idy+1) < img(idx,idy)) % 3
 V(vidx,vidy) = img(idx-1,idy+1);
 end
 if(img(idx,idy+1) < img(idx,idy)) % 4
 V(vidx,vidy) = img(idx,idy+1);
 end
 if(img(idx+1,idy+1) < img(idx,idy)) % 5
 V(vidx,vidy) = img(idx+1,idy+1);
 end
 if(img(idx+1,idy) < img(idx,idy)) % 6
 V(vidx,vidy) = img(idx+1,idy);
 end
 if(img(idx+1,idy-1) < img(idx,idy)) % 7
 V(vidx,vidy) = img(idx+1,idy-1);
 end
 vidy = vidy+1;
 end
 sidx = sidx+1;
 sidy = 1;
 uidx = uidx+1;
 uidy = 1;
end
%Thresholding
% level= graythresh(C);
% B= im2bw(C,level); % between zero and one
B=C;
%mL=200; %Threholding applied manually to
image matrix B
mL=mean2(C);
B(B>mL)=255;
B(B<mL)=0;
B2= B + S; % Adding Selected image and
Thresholding Image
subplot(2,3,1);
imshow(S); %selected
subplot(2,3,2);
 
imshow(U); %max
subplot(2,3,3);
imshow(V); %min
subplot(2,3,4);
imshow(C); %Diff
subplot(2,3,5);
imshow(B); %Thresholding done
subplot(2,3,6);
imshow(B2);
pause;
close;
opticalga; % CALL for next program to calculate Ga
 
Last edited by a moderator:
Physics news on Phys.org
Please copy-paste the errors too, we can't help unless we see the errors and in which line they are occurring. MATLAB isn't concerned about indentation, unlike Python.
 
Back
Top