imfreehand data type issue - matlab

I'm using imfreehand in my program, and when I came to erosion, I got the following error (I didn't paste the whole error):
Error using imerode
Expected input number 1, IM, to be one of these types:
numeric, logical
Instead its type was imfreehand.
It seems then that the region I extract will be of type imfreehand? Is there a way to convert to the above data types? Or, there is some way to deal with such issue?
Thanks.

You can take a look at the examples in this post. My interpretation is that you create a mask using the handle associated with the generated freehand object,
hFH = imfreehand();
binaryImage = hFH.createMask();
then use the mask for additional purposes.

Related

It appears imageLabeler cannot handle a transformed datastore?

The Matlab app imageLabeler is supposed to support the following format:
imageLabeler(imgStore)
I have an imgStore, defined as follows:
imds = imageDatastore(cellArrayOfImageFilenames);
imgStore = transform(imds, #(x)demosaic(x,'rggb'));
I have to do this, because my images are stored as bayer encoded images, and this is the only way I've figured out to get the imgStore to return these images as 3 channel RGB images. However, when I try and initalize imageLabeler, I get this error:
>> imageLabeler(imgStore)
Error using imageLabelerInternal
Expected input name to be one of these types:
char
Instead its type was matlab.io.datastore.TransformedDatastore.
Error in vision.internal.imageLabeler.imageLabelerInternal
Error in imageLabeler (line 58)
vision.internal.imageLabeler.imageLabelerInternal(varargin{:});
TLDR:
How do I get imageLabeler to handle my bayer encoded images?
The way to fix this, is with the imageDatastore 'ReadFcn' parameter. The documentation for imageDatastore explicity tells you to NOT do this, as it slows down Neural Network stuff. Here's the Matlab doc text:
Using ReadFcn to transform or pre-process 2-D images is not
recommended. For file formats recognized by imformats, specifying
ReadFcn slows down the performance of imageDatastore. For more
efficient ways to transform and pre-process images, see Preprocess
Images for Deep Learning (Deep Learning Toolbox).
So, all that said, here's the workaround:
imgStore = imageDatastore(cellArrayOfImageFilenames ...
, 'ReadFcn', #(x)demosaic(imread(x),'rggb')));

How to recognize and print the segmented character

I'm applying some image processing techniques in MATLAB I was able to segment the license plate as show in the figure below:
Now if I apply the followig code in a for loop:
ocrResults = ocr(finalImage);
ocrResults.Text
I'm getting output like VV, u etc that means these characters are not recognized properly. So, how can I fix that? It's not mandatory to use the OCR class so any other solution will also work.
MATLAB's ocr function accepts additional inputs as Name/Value pairs. In your case, to limit the output to numeric values, simply add in the following parameters:
ocrResults = ocr( finalImage, 'CharacterSet', '0123456789' );
However, I'm not certain doing just this will get you the output you desire. It might be helpful to erode the image and add additional blackspace around each character. Take advantage of other possible input parameters which may be added, such as 'TextLayout'.

Adaptive thresholding Matlab-Opencv

I wanted to apply cv.adaptiveThreshold (opencv) through matlab on 8-bit single-channel uint8 image but I am always getting error. I have tried following code from this website: https://kyamagu.github.io/mexopencv/matlab/adaptiveThreshold.html
th = cv.adaptiveThreshold(img,255,'Method',mean,'Type',binary,3,2);
and it gives me the error of Not enough input argument.
when I am running like this:
th = cv.adaptiveThreshold(img);
it is working properly but this is not what I want. I don't want to apply default methods and criteria.
Thanks for the help.
Read the documentation. You are not following the function argument list
cv.adaptiveThreshold(img,'MaxValue',255,'Method','Mean','Type','Binary','BlockSize',3,'C',2);
should work

Summing the image matrix matlab

I am new to matlab . Can someone please tell me whats wrong with this snippet for summing the 3-D array of image.Its showing error in 3rd line and I am unable to debug it.
x=imread('test.jpg');
imshow(x);
sumdiff=sum(sum(sum(testArr2, 3),2),1)
The only thing that strikes me as obviously wrong is that you are summing over the values in a variable called testArr2 but have the image pixel data in a variable called x. Where does testArr2 get defined and populated with data ?
While Mark had probably answered the question, I wanted to add that the easiest way to sum over the entire array is probably using the colon syntax:
sum_all = sum(x(:))
Also note that imread usually returns an array of integers (uint8 for standard jpeg images). Not all mathematical operations are allowed when using this type of arrays - and sometimes using im2double is necessary.

Problem with Array type "DAMPAR" in MATLAB deconvolucy.m

Below is part of the code that i tried to edit from, MATLAB's deconvolucy.
it appears to have problem with DAMPAR where the class type does not match.
can anyone help or does anyone know a better way to call in an image that I (as in deconvolucy.m) would tolerate?
[perhaps i should convert the image into array before use? how do i do so?]
// -- code -- //
I = imread('C:\Users\Lem\Desktop\III\TIFF\69_M.000.tif', 'tif');
class(I)
PSF = fspecial('gaussian',7,10);
V = .0001;
BlurredNoisy = imnoise(imfilter(I,PSF),'gaussian',0,V);
WT = zeros(size(I));
WT(5:end-4,5:end-4) = 1;
J1 = deconvlucy(BlurredNoisy,PSF);
J2 = deconvlucy(BlurredNoisy,PSF,20,sqrt(V));
J3 = deconvlucy(BlurredNoisy,PSF,20,sqrt(V),WT);
//.........//
??? Error using ==> deconvlucy>parse_inputs at 316
In function deconvlucy, DAMPAR has to be of the same class as the input image.
Error in ==> deconvlucy at 102
[J,PSF,NUMIT,DAMPAR,READOUT,WEIGHT,SUBSMPL,sizeI,classI,numNSdim]=...
You have read in an image using imread. So it is probably coming in as uint8? The help for imread says the result will be integer of some order for a tiff image. What class was I when it was returned?
You then filtered the image. It appears that imfilter will return an integer image for an integer input image.
Next, you add noise, using imnoise. From the online help for imnoise, it internally converts the image to a [0,1] (double) number, adds the Gaussian noise, then converts back to integer output. So your blurred image should still be integer, probably uint8 elements.
The help for fspecial says it will return a double precision output for PSF.
You called deconvolucy with only two arguments, so it is using the default value for DAMPAR. (I'll argue that this should not fail here. The author of deconvolucy appears not to have supplied a default value that was consistent in type with the inputs.)
Not knowing enough about the IPT or deconvolucy, I might first suggest re-running this code, using two different calls.
J1 = deconvlucy(BlurredNoisy,PSF,[],0);
J1 = deconvlucy(BlurredNoisy,PSF,[],uint8(0));
If one of these calls did not fix the problem, it suggests that deconvolucy expects a double input for the image, BlurredNoisy. The online help for deconvolucy was not specific here. It says only that I may be an N dimensional array or a cell array. Further on in the help, it calls the result a numeric array. So I believe that the image for deconvolucy is expected to be a floating point image. (By my standards, this is a flaw in the help.)
I would then probably try scaling your image to [0,1] as a double. It is just a guess however. So something like:
BlurredNoisy = double(BlurredNoisy)/255;
This assumes your image was uint8 in class originally.