How to make a GIF from a set of MATLAB images - matlab

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

Related

DIPimage measure missing argument

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

Making a copy of an image via a Loop not working

Idk why the code won't work. All it does is give me a blank white image. And if you don't declare a matrix before by zeros(x, y) then it works fine. Wth is wrong here?
I tried not declaring the zeros matrix before and only that works. I even tried doing img2(i,j) = img2(i,j)+img1(i,j)
function [imgOut] = scaleLoopBased(img,s)
%UNTITLED4 Summary of this function goes here
% creating a zero matrix of the given scale
[rows,columns]=size(img);
imgTemp=zeros(rows, columns);
for i=1:rows
for j=1:columns
imgTemp(i, j) = img(i, j);
end
end
imshow(imgTemp);
imgOut = imgTemp;
end
Blank white image
This is a result of your new image (of type double, what zeros creates by default) not having the same type as your original image (typically type uint8). You can fix this by initializing your new image to have the same data type as the original using the class function and passing an additional argument to zeros:
imgTemp = zeros(rows, columns, class(img));
The reason it works correctly when you don't initialize imgTemp is because MATLAB initializes the variable for you using the data type of img by default when you perform the first indexed assignment.
The imshow utility expects one of the standard image types in MATLAB. An image of type double is expected to have values spanning the range [0 1], while images of type uint8 will have values in the range [0 255]. In your example you likely have a matrix imgTemp that is of type double but has values spanning [0 255]. Another way to fix your issue is to explicitly tell imshow what value range to use for the display (since the default [0 1] doesn't work):
imshow(imgTemp, [0 255]);
Always be aware of the data type when manipulating or processing images. You may need to scale or convert back and forth, using a double type for calculations (since integers saturate) and a uint8 type for display and reading/writing to files.

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:

Using imfreehand within your program

For instance, I start my program as follows reading some image:
I=input('image name: ','s');
img=double(imread(I));
I'm planning to work only on some portion of that image. So, I noticed that I may need h=imfreehand for this purpose.
Thus, I have inserted h=imfreehand under the two lines above. What I got is a white screen. So, how can I get the image and select the region I want? The other thing is, how can I tell my program to work only on that region I have selected?
Thanks.
UPDATE
I did the following in a portion of my code:
figure, imshow(img);
h = imfreehand;
position = wait(h);
% post processing
se=strel('disk',3);
erosion=imerode(h,se);
result_image=imsubtract(h,erosion);.
But, I got the following error:
Error using imerode
Expected input number 1, IM, to be one of these types:
numeric, logical
Instead its type was imfreehand.
Error in morphop>CheckInputImage (line 335)
validateattributes(A, {'numeric' 'logical'}, {'real' 'nonsparse'}, ...
Error in morphop>ParseInputs (line 166)
A = CheckInputImage(A, func_name);
Error in morphop (line 14)
[A,se,pre_pad,...
Error in imerode (line 123)
B = morphop(A,se,'erode',mfilename,varargin{:});
Error in program (line 155)
erosion=imerode(h,se);
Is it to do with erosion? What can be done in this case?
`
Following the advice in the matlab documentation try this:
I=input('image name: ','s');
img=imread(I);
figure, imshow(img);
h = imfreehand;
position = wait(h);
Edit:
The documentation suggests as an alternative
figure, imshow(img);
h = imfreehand(gca);
Try passing the handle of the plot to imfreehand()
I=input('image name: ','s');
img=double(imread(I));
figure;
ih=image(img);
h=imfreehand(ih)
Sorry, I don't have the image processing toolbox to test this.
Seems like converting your image (possibly uint8) to double causes a problem.
I would either do:
use the original coding of the image (for example img_uint8 = imread('coins.png') is coded using integers). The problem is that you won't probably be able to use imfreehand as it needs to read double or single precision float.
convert the image using img_double = double(imread('coins.png')) so that imfreehand will work. The conversion however causes a display problem that you can bypass with imshow(img_double,[]) that is an analogue of imshow(img_double, [min(min(img_double)), max(max(img_double))]). It forces imshow to correctly use the full range of data (255 Vs. 255.0)
The best option is therefore the second one.
To use imfreehand, #Try Hard gave a nice code h = imfreehand or h = imfreehand(gca)

Insert text characters into image pixels

I am looking for a code to insert characters into image with matlab.
The img is my cover image and msg is my text. I get this error
Error using ==> bitset
Operands to BIT Ops must be numeric.
But mine is numeric. It is not? How do I make it clear?
clear all;
img=imread('img.jpg','jpg');
msgopen=fopen('ramz.txt');
msg=fread(msgopen);
msgbin=dec2bin(msg);
msgsize=size(msg);
x=msgsize(1);
msgsizebin=dec2bin(x,8);
imgh=size(img,1);
imgw=size(img,2);
for i=1:imgh;
for j=1:imgw;
img(i,j)=bitset(img(i,j),msgbin(i,1));
end
end
You can use the text function and then the hardcopy function to create the image with the text coded into the data;
img=imread('img.jpg','jpg');
msgopen=fopen('ramz.txt');
msg=fread(msgopen);
hfig=figure;
imshow(img);
hold on;
text(x_loc,y_loc,msg); %x_loc and y_loc are the locations where you want the text to
% appear. msg is assumed to be a string.
orig_mode = get(hfig, 'PaperPositionMode');
set(hfig, 'PaperPositionMode', 'auto');
img_with_text = hardcopy(hfig, '-Dzbuffer', '-r0');