How to make .jpg file two-dimensional?

I have just written a coda but it had a problem that my image is not two dimensional. What could be a problem? Could someone say where I could learn Matlab from? (code given below)
A=imread ('phb_rt_121011_first-try_26.jpg');
bw = bwareaopen(A,30);
imshow(bw);
??? Error using ==>
imageDisplayValidateParams>validateCData
at 119
If input is logical (binary), it must be
two-dimensional.
Error in ==> imageDisplayValidateParams
at 31
common_args.CData =
validateCData(common_args.CData,image_type);
Error in ==> imageDisplayParseInputs at
79
common_args =
imageDisplayValidateParams(common_args);
Error in ==> imshow at 199
[common_args,specific_args] = ...

1 Comment

Also changing the extension to .tif in paint works

Sign in to comment.

Answers (2)

JPEG files can never be 2 dimensional: the JPEG file format is defined as being an RGB file format, in which there are 3 channels per pixel (and thus a total of 3 dimensions.)
bwareaopen() is only defined in binary arrays. When you feed in the 3D array that is the RGB intensities for each pixel in your array, bwareaopen() is going to treat each intensity that is 0 as being "black" and is going to treat every other intensity as being "white", which is the standard MATLAB conversion: 0 is false, non-zero is true.
The output of bwareaopen() is going to be binary (logical). But because you fed in a 3D matrix, the output is going to be a 3D matrix of logical. Which imshow() cannot handle.
What most people do with their images is convert them to grayscale using rgb2gray(), and then they threshold the image to black-and-white using a threshold value that depends on what they are trying to do with the image. Your task might require that the image be processed a different way completely.
class(A)
will return 'uint8' (most likely). Calling 'bw'areaopen will want a black and white logical image. This is dangerous. Convert your image to black and white first. Maybe:
A = rgb2gray(A);
A = A>50; %values greater than 50 are white

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

LG9
on 25 Oct 2011

Commented:

on 23 May 2020

Community Treasure Hunt

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

Start Hunting!