Jaskehar S.
asked 07/26/23Matlab Code png to binary
I am having trouble converting my PNG image to binary. The image name is correct, but I am getting a confusing error on line 4. I put the code with the error mesage below. Thank you.
fullFileName = 'MatLabimage.png';
if exist(fullFileName, 'file')
rgbImage = imread(fullFileName);
binaryImage = im2bw(rgbImage, 0.4);
subplot(1, 2, 1);
imshow(rgbImage)
subplot(1, 2, 2);
imshow(binaryImage);
else
errorMessage = sprintf('Image file does not exist:\n%s', fullFileName);
uiwait(warndlg(errorMessage));
end
Unrecognized function or variable 'im2bw'.
Error in Binary3 (line 4)
binaryImage = im2bw(rgbImage, 0.4);
1 Expert Answer
Ryan G. answered 08/04/23
PhD Student Highly Experienced in Python & MATLAB for Data Science
Do you have the Image Processing Toolbox installed? The error you're getting indicates that the 'im2bw' function is not found in your current MATLAB path and needs to be installed via this toolbox (https://www.mathworks.com/help/images/index.html?s_tid=CRUX_lftnav).
Once you've installed the toolbox, the MATLAB documentation for 'im2bw' (https://www.mathworks.com/help/images/ref/im2bw.html) indicates that this function is no longer recommended and suggests replacing it with 'imbinarize'. You should try running this code instead:
fullFileName = 'MatLabimage.png';
if exist(fullFileName, 'file')
rgbImage = imread(fullFileName);
binaryImage = imbinarize(rgbImage, 0.4);
subplot(1, 2, 1);
imshow(rgbImage)
subplot(1, 2, 2);
imshow(binaryImage);
else
errorMessage = sprintf('Image file does not exist:\n%s', fullFileName);
uiwait(warndlg(errorMessage));
end
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Joseph H.
What version of matlab are you using? This function also requires the image processing toolbox, you can check if it is installed with the “ver” command. im2bw is also deprecated, check out imbinarize instead. https://www.mathworks.com/help/images/ref/imbinarize.html07/31/23