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

In summary, the user is experiencing difficulties with a code related to image enhancement in Matlab, encountering errors that prevent successful execution.
  • #1
Saikiran
1
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
  • #2
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.
 

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

What are common reasons for Matlab to throw errors while running image enhancement code?

Common reasons include syntax errors, using unsupported functions, incorrect data types, or mismatched dimensions in matrices. It's essential to check for typos and ensure that all functions are properly defined and compatible with the input data.

How can I debug my Matlab code to identify the source of the error?

You can use the built-in debugger in Matlab by setting breakpoints in your code. This allows you to step through the code line by line and inspect variable values. Additionally, using the 'disp()' function can help print out variable states at different points in your code.

What should I do if I encounter an "Index exceeds matrix dimensions" error?

This error typically occurs when you try to access an element outside the bounds of a matrix. Check the indices you are using to access elements and ensure they are within the valid range. You can use the 'size()' function to verify the dimensions of your matrices.

Are there any specific functions or toolboxes in Matlab that are useful for image enhancement?

Yes, the Image Processing Toolbox in Matlab provides several functions specifically designed for image enhancement, such as 'imadjust()', 'histeq()', and 'adapthisteq()'. Ensure that you have this toolbox installed and properly referenced in your code.

How can I find help or documentation for specific Matlab functions related to image enhancement?

You can access the documentation for specific functions by using the 'help' command in the Matlab command window, such as 'help imadjust'. Additionally, the Matlab documentation website offers comprehensive guides and examples for all functions, including those related to image processing and enhancement.

Similar threads

Replies
1
Views
1K
Replies
1
Views
2K
Replies
1
Views
3K
Replies
10
Views
2K
Replies
1
Views
2K
Replies
2
Views
10K
Back
Top