DIPimage measure missing argument - matlab

I am trying to use DIPimage to get some measurements of each object in an image and I get this error:
Error using dip_measure
DIPlib Error in function dip_Measure.
DIPlib Error in function dip_ImageCheck: Data type not supported
Error in measure (line 209)
data = dip_measure(object_in,gray_in,measurementID,objectIDs,connectivity);
Error in Untitled (line 13)
msr = measure(b, [], ({'size', 'perimeter','podczeckShapes'}))
How can I solve it?
Code:
Image = rgb2gray(imread('pillsetc.png'));
BW = imbinarize(Image);
BW = imfill(BW,'holes');
imshow(BW);
[B,L] = bwboundaries(BW,'noholes');
k = 1;
b = B{k};
y = b(:,2);
x = b(:,1);
msr(k) = measure(BW, [], ({'size', 'perimeter','podczeckShapes'}))
sz = msr.size;
podczeckShapes = podczeckShapes;

One problem with your code is the call to imfill. Because the image has bright values all around the image, it is considered that there's a large object with a hole, and your actual objects are inside this hole. imfill fills the hole, leaving the whole image white.
Instead, I suggest the following code to remove the frame:
Image = rgb2gray(imread('https://i.stack.imgur.com/fmqAF.jpg'));
BW = imbinarize(Image);
BW = BW - bpropagation(false(size(BW)), BW);
Because we used a filter in DIPimage, the BW variable now contains a dip_image object, not a normal MATLAB array. dip_array(BW) extracts the normal MATLAB array that is inside. The dip_image object behaves differently from a MATLAB array. For example, you can display it to an interactive figure window by just typing its name:
BW
Next, we apply labeling so that we know which object ID in the measurement data corresponds to which object:
lab = label(BW);
dipshow(lab,'labels')
Now we can apply the measurement function. If we use BW as input, label will be called on it. Since we already have that result, let's use it directly:
msr = measure(lab, [], {'size', 'perimeter','podczeckShapes'});
Let's examine results for object ID 8, which is the large square:
sz = msr(8).size
square = msr(8).podczeckShapes(1)
triangle = msr(8).podczeckShapes(3)
There are other things you can do with the measurement structure, I suggest you read the documentation. For example, we can remove from it the measurement for the littlest objects, which to me look like noise:
msr = msr(msr.size>100); % remove measurement for noise

Related

How to make a GIF from a set of MATLAB images

I have an array of values which I turn into a color image like so:
my_img = imagesc(my_data(:,:,tidx)); axis off; colormap('default'); c=colorbar; c.FontSize=26; c.FontWeight='bold';
I want to know if it's possible to loop through all the index values in my_data and turn them into a GIF.
I tried this:
for tidx = 5:64
my_img = imagesc(my_data(:,:,tidx)); axis off; colormap('default'); c=colorbar; c.FontSize=26; c.FontWeight='bold';
imwrite (my_img, int2str(tidx)+"_cspattern.png");
end
Which gave me this error:
Error using imwrite (line 427)
Expected DATA to be one of these types:
numeric, logical
Instead its type was matlab.graphics.primitive.Image.
I looked it up and tried this solution out:
for tidx = 5:64
my_img = imagesc(my_data(:,:,tidx)); axis off; colormap('default'); c=colorbar; c.FontSize=26; c.FontWeight='bold';
IMG_to_write = uint8(normalize(my_img, 'range', [0 255]));
imwrite(IMG_to_write, int2str(tidx)+"_cspattern.png");
end
But that just gave me this error:
Error using normalize>checkSupportedArray (line 159)
Invalid data type. First argument must be a real numeric or logical array, a table or a timetable.
Error in normalize (line 86)
checkSupportedArray(A,method,methodType,false);
Error in GenerateFigures (line 18)
IMG_to_write = uint8(normalize(SavePattern, 'range', [0 255]));
I feel as if I am overthinking this because there must be a simple way to load the data I want and save it as an image or series of images.
imagesc produces a graphics object, not a data array. It’s meant for displaying images into figures, not saving them into files. You need to hand imwrite your image data directly, along with your desired color map.
for tidx = 5:64
imwrite(my_data(:,:,tidx),parula(),int2str(tidx)+"_cspattern.png");
end
If you want to scale each image so that the full color range is used, the way imagesc does, you can do this manually.
for tidx = 5:64
my_data_slice = double(my_data(:,:,tidx));
my_data_slice = my_data_slice - min(my_data_slice,[],'all');
my_data_slice = my_data_slice / max(my_data_slice,[],'all')*255;
my_data_slice = uint8(my_data_slice);
imwrite(my_data_slice,parula(),int2str(tidx)+"_cspattern.png");
end

Unable to save iverseftt image and image K-Space in matlab

I have a brain MRi image in .png format. I read the image and extract the K-Space and set some of the K-Space as 0
img_fft = fftshift(fft2(img));
sizes = size(img_fft);
row_half = sizes(1)/2;
flag = true;
for r = row_half:sizes(1)
for c = 1:sizes(2)
img_fft(r,c) = 0+1i*0;
end
end
After this I change the image back to image space using
img_back = ifft2(ifftshift(img_fft));
and after this I cast the image to uint8 as that was the original image format.
When I try to plot my image using imshow() I get a different output compared to when I write the image using imwrite. Also if I use abs(img_back) in imwrite I get an error.
Error using abs: Complex integers are not supported.
My plotting code is below:
img_back = ifft2(ifftshift(img_fft));
img_back = cast(img_back,'uint8');
subplot(1,3,1), imshow(img)
subplot(1,3,2), imshow(img_back)
subplot(1,3,3), imshow(abs(img_fft),[])
imwrite(abs(img_back),'back_img.png','png')
Can someone tell me what I am doing wrong here?
Take absolute value after inverse Fourier transform and then cast the result to uint8 type:
img_back = abs(ifft2(ifftshift(img_fft)));
img_back = cast(img_back,'uint8');

Custom pixel information in MATLAB figure

I have generated an array of processed data for each pixel in my image. I want to use the impixelinfo function to show the pixel information. However, it only shows the RGB value with respective x and y coordinates. How can I append the processed data into the info box?
Example of the RGB info box
Here is my answer:
imshow(uint8(image));
dcm_obj = datacursormode(gca);
set(dcm_obj,'UpdateFcn',{#myupdatefcn,image,other parameter});
function txt = myupdatefcn(empt,event_obj,image,other parameter)
pos = get(event_obj,'Position');
img = image(pos(2),pos(1),:);
txt = {['X:',num2str(pos(1)),' Y:',num2str(pos(2))],...
['R:',num2str(img(1,1,1)),' G:',num2str(img(1,1,2)),' B:',num2str(img(1,1,3))]
};
end

Embedding data from a text file into DWT subband using steganography

I am attempting to embed data from a text file(which contains only numeric data) into LL subband of an image using a steganography. I am getting an error "Error using bitset ASSUMEDTYPE must be an integer type name" in the line of code:
L(ii,jj)=bitset(L(ii,jj),1,stego(ii,jj));
I have attempted to run in debugger but I am having no luck. I think it must be something to do with the data type of L?? I have tried changing image to binary,double etc but I still get this error! Please can someone give me some advice on where I am going wrong?I have a insert my code below
% loading cover image
img=imread('lena.bmp');
image=im2double(img);
% get DWT of image
[LL,LH,HL,HH] = dwt2(image,'haar');
A = importdata('minutiaTest.txt');
I = dec2bin(A,8);
L=LL;
% determine size of LL subband
Mc=size(L,1); %Height
Nc=size(L,2); %Width
% determine size of message object
Mm=size(I,1); %Height
Nm=size(I,2); %Width
for ii = 1:Mc
for jj = 1:Nc
stego(ii,jj)=I(mod(ii,Mm)+1,mod(jj,Nm)+1);
end
end
for ii = 1:Mc
for jj = 1:Nc
L(ii,jj)=bitset(L(ii,jj),1,stego(ii,jj));
end
end
stego_image = idwt2(LL,LH,HL,HH,'haar');
imwrite(uint8(stego_image),'stego.bmp');
figure; imshow(stego_image,title('Stego image'));

Matlab get vector of specific pixels

I am pretty new to Matlab and encountered a problem when working with images.
I want to get a pixel that is in a specific colour (blue) in the following image:
image
My current code looks something like this:
function p = mark(image)
%// display image I in figure
imshow(image);
%// first detect all blue values higher 60
high_blue = find(image(:,:,3)>60);
%cross elements is needed as an array later on, have to initialize it with 0
cross_elements = 0;
%// in this iteration the marked values are reduced to the ones
%where the statement R+G < B+70 applies
for i = 1:length(high_blue)
%// my image has the size 1024*768, so to access the red/green/blue values
%// i have to call the i-th, i+1024*768-th or i+1024*768*2-th position of the "array"
if ((image(high_blue(i))+image(high_blue(i)+768*1024))<...
image(high_blue(i)+2*768*1024)+70)
%add it to the array
cross_elements(end+1) = high_blue(i);
end
end
%// delete the zero element, it was only needed as a filler
cross_elements = cross_elements(cross_elements~=0);
high_vector = zeros(length(cross_elements),2);
for i = 1:length(cross_elements)
high_vector(i,1) = ceil(cross_elements(i)/768);
high_vector(i,2) = mod(cross_elements(i), 768);
end
black = zeros(768 ,1024);
for i = 1:length(high_vector)
black(high_vector(i,2), high_vector(i,1)) = 1;
end
cc = bwconncomp(black);
a = regionprops(cc, 'Centroid');
p = cat(1, a.Centroid);
%// considering the detection of the crosses:
%// RGB with B>100, R+G < 100 for B<150
%// consider detection in HSV?
%// close the figure
%// find(I(:,:,3)>150)
close;
end
but it is not optimized for Matlab, obviously.
So i was wondering if there was a way to search for pixels with specific values,
where the blue value is larger than 60 (not hard with the find command,
but at the same time the values in the red and green area not too high.
Is there a command I am missing?
Since English isn't my native language, it might even help if you gave me some suitable keywords for googling ;)
Thanks in advance
Based on your question at the end of the code, you could get what you want in a single line:
NewImage = OldImage(:,:,1) < SomeValue & OldImage(:,:,2) < SomeValue & OldImage(:,:,3) > 60;
imshow(NewImage);
for example, where as you see you provide a restriction for each channel using logical operators, that you can customize of course (eg. using | as logical OR). Is this what you are looking for? According to your code you seem to be looking for specific regions in the image like crosses or coins is that the case? Please provide more details if the code I gave you is completely off the track :)
Simple example:
A = imread('peppers.png');
B = A(:,:,3)>60 & A(:,:,2)<150 & A(:,:,1) < 100;
figure;
subplot(1,2,1);
imshow(A);
subplot(1,2,2)
imshow(B);
Giving this: