I have already uploaded the image. I need to answer below questions.
a. Load the “cameraman” image. Convert it to a double array. Determine maximum (Imax) and the minimum (Imin) pixel value of the image
b. Write a code to rescale the image such that all pixels with value< 1.25*Imin are equated to 1.25*Imin and all pixels with value > 0.75*Imax are equated to the 0.75*Imax.
c. Display the new image.
I have tried below codes but all I get is a black image.
**A = imread('D:\Matlab files\BRAIN 180\IMG-0002-00067.bmp','bmp')
I = rgb2gray(A);
I2 = double(I)/255;
%subplot(2,2,1)
%imshow(I2)
Imin=min(I2(:))
Imin
Imax=max(I2(:))
Imin
for i=1:256
for j=1:256
if I2(i,j)<1.25;
I2(i,j)=1.25*Imin;
else
I2(i,j)=0.75*Imax;
end
end
end
imshow(I2)**
Your question asks for "value< 1.25*Imin" but in your code you wrote I2(i,j)<1.25, the Imin is missing.
Second problem, the condition "value > 0.75*Imax" is required by the question but not found in your code.
Related
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:
I am writing some Matlab code to analyze spikes and their stimuli. In the first part of the code I get the timing of a spike and then find the frame that was shown on the screen just prior. I then want to take the image data from that frame and add it to my own movie data. The x and y components of myMovie and moviedata are both 128x128, however when I try to add the image values from a certain frame from the stimuli moviedata(:,:,j) to a specific frame in my movie myMovie(:,:,k) I get "error: matrix dimensions must agree". Is there any way to fix this?
Code:
for n=1:1100
t = blocks(5).spikes{1}(:,n);
for k=1:25
ind = find(round(double(blocks(5).frameEpocs*1000)/1000) == (t-(1/(25*10))));
j = blocks(5).frameEpocs(1,ind);
myMovie(:,:,k) = myMovie(:,:,k) + double(moviedata(:,:,j));
end
end
find may return an empty matrix and hence j would be empty as well.
In that case, the command myMovie(:,:,k) + double(moviedata(:,:,j)); attempts to add a 128x128x1 matrix with a 128x128x0 matrix and thus the error you got.
As a follow up to my question here:
What does createMask actually do? I went to the description from MathWorks here, but wan't much clear.
If you see in the answer of my question referenced above: img2(roi.createMask) = 1;, the part roi.createMask reminds me of function call, is that what we are really doing here? Calling the createMask function?
Thanks.
In the code
img = im2double(imread('cameraman.tif'));
imshow(img);
roi = imfreehand(gca);
img2 = img;
img2(roi.createMask) = 1;
imshow(img2);
roi is the handle to the object generated by imfreehand. One of the methods (~functions) available through the object (using the handle) is createMask, which can be accessed with the . operator. The method generates a type logical array of the same size as the pixel dimensions of the image. Values in the logical array are either 1 or 0 with values of 1 assigned to entries in the region corresponding to the area selected with the imfreehand operation. The operation img2(roi.createMask) =1; indexes into the image img2 (it picks elements in img2) using the positions in the logical array with value 1 and assigns those elements value 1.
So I have a certain grayscale image as a binary file. After I red in the image, I tried to create a series of "if" loops in order to replace a range of values with one value, and leave the rest of the matrix untouched.
I used this code
if myimage < 20
myimage = 0;
else if 20 < myimage <40
myimage = 20;
else if 40 < myimage < 60
myimage = 40;
else if 60<myimage<80
myimage = 60;
end
end
end
end
but for some reason it failed to load an image. After some debugging I figured out that the file was becoming a 1 x 1 matrix with the value "20" after the "else if 20...." line. Can anyone help me figure out why exactly this is happening? thanks.
You need to change the specific indices in myimage that have a specific value. The way your currently calling it, you're overwriting the myimage variable with a specific value. One way to find all the relevant indices is with find:
find(myimage==20)
in order to find and replace all the values with a one liner, reference the indices of interest in myimage:
myimage(find(myimage<20))=0;
and to combine multiple sets of indices (eg values >20 AND <40), use intersect:
myimage(intersect(find(myimage>20),find(myimage<40)))=20;
I have a proplem when dealing with images in matlab, i have a white image and when i try to print the gray level of the image and increment it by 1 , it gives me 255, it never give me 256.
and here is the code. and the count is 0.
function [ count ] = white( I )
[row,col]=size(I);
count=0;
for x=1:row
for y=1:col
g=I(x,y); %the value of the gray level on each pixel
if((g+1) == 256)
count=count+1;
256
end
end
end
Your image class is probably uint8 and 255 is the maximal value of this class . For example:
>> uint8(inf)
ans =
255
Instead try to cast to a different class, for example I=uint32(I) ...
Following #Aganders3, I'll also offer a solution to your code that doesn't use for loops:
count=sum(I(:)>threshold); % Credit to #Jonas and #Aganders3
where threshold is the gray level you want to threshold
I think nate is correct on why this is not working.
Also, consider a much simpler solution to your problem (given I is full of integers):
count = sum(vector(I == intmax(class(I))));