A = imread('ab.jpg');
A = imresize(A,[255 255]); subplot(2,2,1), imshow(A);
B = imread('cd.jpg');
B = imresize(B,[255 255]);subplot(2,2,2), imshow(B);
C = imadd(A,B);subplot(2,2,3),imshow(C);
This is the program i have used to add two images and it is working fine. Now i want to get back both the images. Am not able to figure out how to get it back without taking one of the images as reference (ie subtracting image A from the sum to get image B). Can anyone please help?
Let's not worry about images. Say I have two real integers, x and y. I add them to get z = x+y. Now if I tell someone the integer z is there any way for him or her to get back x and/or y?
Potentially you can retrieve one of the numbers out of the summation, by having the other. But the issue you're facing with is the fact that your image matrices cannot exceed the value 255 since MATLAB uses uint8 type by default. Here is an example for two corresponding pixels in image A and B:
% A(1,1) == 130 is ture
% B(1,1) == 180 is ture
C(1,1) = A(1,1) + B(1,1); %C(1,1) == 310 is false! c(1,1) == 255 is true
expectedB = C(1,1) - A(1,1); % expectedB == 180 is false! expectedB == 125 is true
You can cast the type of your matrices to int16. Whereas by doing so MATLAB's functions sich as imshow do not work as you want them to. Because they assume that your
Related
I have a matrix named figmat from which I obtain the following pcolor plot (Matlab-Version R 2016b).
Basically I only want to extract the bottom red high intensity line from this plot.
I thought of doing it in some way of extracting the maximum values from the matrix and creating some sort of mask on the main matrix. But I'm not understanding a possible way to achieve this. Can it be accomplished with the help of any edge/image detection algorithms?
I was trying something like this with the following code to create a mask
A=max(figmat);
figmat(figmat~=A)=0;
imagesc(figmat);
But this gives only the boundary of maximum values. I also need the entire red color band.
Okay, I assume that the red line is linear and its values can uniquely be separated from the rest of the picture. Let's generate some test data...
[x,y] = meshgrid(-5:.2:5, -5:.2:5);
n = size(x,1)*size(x,2);
z = -0.2*(y-(0.2*x+1)).^2 + 5 + randn(size(x))*0.1;
figure
surf(x,y,z);
This script generates a surface function. Its set of maximum values (x,y) can be described by a linear function y = 0.2*x+1. I added a bit of noise to it to make it a bit more realistic.
We now select all points where z is smaller than, let's say, 95 % of the maximum value. Therefore find can be used. Later, we want to use one-dimensional data, so we reshape everything.
thresh = min(min(z)) + (max(max(z))-min(min(z)))*0.95;
mask = reshape(z > thresh,1,n);
idx = find(mask>0);
xvec = reshape(x,1,n);
yvec = reshape(y,1,n);
xvec and yvec now contain the coordinates of all values > thresh.
The last step is to do some linear polynomial over all points.
pp = polyfit(xvec(idx),yvec(idx),1)
pp =
0.1946 1.0134
Obviously these are roughly the coefficients of y = 0.2*x+1 as it should be.
I do not know, if this also works with your data, since I made some assumptions. The threshold level must be chosen carefully. Maybe some preprocessing must be done to dynamically detect this level if you really want to process your images automatically. There might also be a simpler way to do it... but for me this one was straight forward without the need of any toolboxes.
By assuming:
There is only one band to extract.
It always has the maximum values.
It is linear.
I can adopt my previous answer to this case as well, with few minor changes:
First, we get the distribution of the values in the matrix and look for a population in the top values, that can be distinguished from the smaller values. This is done by finding the maximum value x(i) on the histogram that:
Is a local maximum (its bin is higher than that of x(i+1) and x(i-1))
Has more values above it than within it (the sum of the height of bins x(i+1) to x(end) < the height of bin x):
This is how it is done:
[h,x] = histcounts(figmat); % get the distribution of intesities
d = diff(fliplr(h)); % The diffrence in bin height from large x to small x
band_min_ind = find(cumsum(d)>size(figmat,2) & d<0, 1); % 1st bin that fit the conditions
flp_val = fliplr(x); % the value of x from large to small
band_min = flp_val(band_min_ind); % the value of x that fit the conditions
Now we continue as before. Mask all the unwanted values, interpolate the linear line:
mA = figmat>band_min; % mask all values below the top value mode
[y1,x1] = find(mA,1); % find the first nonzero row
[y2,x2] = find(mA,1,'last'); % find the last nonzero row
m = (y1-y2)/(x1-x2); % the line slope
n = y1-m*x1; % the intercept
f_line = #(x) m.*x+n; % the line function
And if we plot it we can see the red line where the band for detection was:
Next, we can make this line thicker for a better representation of this line:
thick = max(sum(mA)); % mode thickness of the line
tmp = (1:thick)-ceil(thick/2); % helper vector for expanding
rows = bsxfun(#plus,tmp.',floor(f_line(1:size(A,2)))); % all the rows for each column
rows(rows<1) = 1; % make sure to not get out of range
rows(rows>size(A,1)) = size(A,1); % make sure to not get out of range
inds = sub2ind(size(A),rows,repmat(1:size(A,2),thick,1)); % convert to linear indecies
mA(inds) = true; % add the interpolation to the mask
result = figmat.*mA; % apply the mask on figmat
Finally, we can plot that result after masking, excluding the unwanted areas:
imagesc(result(any(result,2),:))
This question already has an answer here:
Efficient inpaint with neighbouring pixels
(1 answer)
Closed 6 years ago.
I am trying to replace all pixels with certain value in an image with the average values of the neighbors. Can interp2 be useful here? I tried this -
I = imread('test_image.JPG');
[r c] = size(I);
class_of_image = class(I);
[xi,yi] = meshgrid(1:0.5:c,1:0.5:r);
I1 = cast(interp2(double(image),xi,yi,'linear'),class_of_image);
[x_z,y_z] = find(I1==0);
I1(x_z,y_z) = I1(x_z-1,y_z)+I1(x_z+1,y_z)+I1(x_z,y_z-1)+I1(x_z,y_z+1);
This fails spectacularly with an error message - Index exceeds matrix dimensions.
I realize that the error is in trying to access I1 indices beyond r and c. Is there a generic way to incorporate this in the code?
Please help!
If you are trying to replace pixels in an image that are at a certain value to be the average of its 4 neighbours, then you don't have to use interp2. It looks like you are doubling the size of the image and then sampling from that image when you're done.
If you want to do what you're asking, you need to use column-major indices to facilitate the vectorized access of pixels. Specifically, you need to use sub2ind to help determine the locations you need to access in your matrix.
However, you will need to account for pixels that go out of bounds. There are many ways to accommodate this, but what I will implement is known as zero-padding where the border pixels are simply set to 0. I would create a zero-padded image where the top and bottom rows as well as the left and right values are all some sentinel value (like -1), use find on this image to find the coordinates then do the inpainting. Make sure you set the border pixels back to 0 before doing this so that you don't use -1 as part of the inpainting. You would then crop the border pixels of this new image when you're done to obtain the final output image.
Therefore, if you want to perform your "inpainting" try this instead:
% Read in image
I = imread('test_image.JPG');
% Create padded image with border pixels set to -1
Ipad = -ones(size(I) + 2);
% Place image in the middle
Ipad(2:end-1,2:end-1) = I;
% Find zero pixels
[r,c] = find(I == 0);
% Now set border pixels to 0
Ipad(Ipad == -1) = 0;
% Find column major indices for those elements that are 0
% as well as their 4 neighbours
ind = sub2ind(size(I), r, c);
ind_up = sub2ind(size(I), r-1, c);
ind_down = sub2ind(size(I), r+1, c);
ind_left = sub2ind(size(I), r, c-1);
ind_right = sub2ind(size(I), r, c+1);
% Perform the inpainting by averaging
Ipad(ind) = (Ipad(ind_up) + Ipad(ind_down) + Ipad(ind_left) + Ipad(ind_right))/4;
% Store the output in I1 after removing border pixels
I1 = Ipad(2:end-1,2:end-1);
However, a possibly shorter way to do this even though you would operate on the entire image would be to perform 2D convolution using a 3 x 3 kernel whose elements are 1 in the cardinal directions and ensuring that you divide by 4 to find the average value per location. After, you would simply copy over those values in the output that are 0 in the original image. You can use conv2 to do that and make sure you specify the 'same' flag to ensure that the output size is the same as the input size. The behaviour of conv2 when you approach the border elements is to zero-pad, which is what I did already in the first implementation:
% Read in image
I = imread('test_image.JPG');
% Specify kernel
kernel = [0 1 0; 1 0 1; 0 1 0] / 4;
% Perform convolution - make sure you cast image to double
% as convolution in 2D only works for floating-point types
out = conv2(double(I), kernel, 'same');
% Copy over those values from the output that match the value
% to be inpainted for the input. Also cast back to original
% data type.
I1 = I;
I1(I == 0) = cast(out(I == 0), class(I));
Could you tell me how to take a range of an image? I want to do a gamma correction of "cameraman.tif" for pixels in range 0:120 gamma = 1.8, for pixels in range 120:255 gamma = 0.5
But all pixels go to first if statement so I am unable to apply second gamma.
a = imread('cameraman.tif');
gamma1 = 2;
gamma2 = 0.5;
N =size(a);
out = zeros(N);
for i=1:N
for j=1:N
temp=a(i,j);
if temp>0&temp<120
out(i,j)=temp.^2;
end
if temp>120&temp<=255
out(kx,ky)=temp.^0.5;
end
end
end
imshow(out)
Your second if statement uses access variables kx and ky.... I'm assuming you wanted to use i and j:
out(i,j)=temp.^0.5;
You also must make sure that the intensity is double precision for the square root to work. Therefore make sure that the intensity read in per location is cast to double, then convert back to uint8 when you're done. Actually, do the conversion after you sweep through the whole image.
for i=1:N
for j=1:N
temp=double(a(i,j)); % Change
if temp>0&temp<120
out(i,j)=temp.^2;
end
if temp>120&temp<=255
out(i,j)=temp.^0.5; % Change
end
end
end
out = uint8(out); % Change
kx and ky were set somewhere else in your code and are never changing, so this means that if and when the second if statement does happen, the setting of the gamma only happens at one spot only defined at kx and ky. My advice to you would be to write an actual function so that you aren't cross-contaminating variables in different workspaces. Encompassing this in a function would have given you an error immediately telling you that kx and ky are not defined.
BTW, I would rather you do this more efficiently without loops. You can very easily perform the same operations vectorized. However, this requires that you convert the image to double as the default type is uint8 for the Cameraman image. Therefore, use double to convert an image to double, do the gamma correction, then convert back using uint8:
a = double(imread('cameraman.tif'));
out = zeros(size(a));
out(a > 0 & a < 120) = a(a > 0 & a < 120).^2;
out(a >= 120 & a <= 255) = a((a >= 120 & a <= 255).^0.5;
out = uint8(out);
The first and second line of code are of course familiar. The third line of code finds a logical mask where we search for intensities between 0 and 120 exclusive. Once we find those values, we use the same logical mask to index into the original image and only access those values, square each value and set them in the same spatial locations at the output. The same can be said for the last line of code where you're searching between 120 and 255 but you are taking the square root instead. We finally convert to uint8 for display.
I am having some problems in matlab i don't understand. The following piece of code analyses a collection of images, and should return a coherent image (and always did).
But since I've put an if-condition in the second for-loop (for optimisation purposes) it returns an interlaced image.
I don't understand why, and am getting ready to throw my computer out the window. I suspect it has something to do with ind2sub, but as far as i can see everything is working just fine! Does anyone know why it's doing this?
function imageMedoid(imageList, resizeFolder, outputFolder, x, y)
% local variables
medoidImage = zeros([1, y*x, 3]);
alphaImage = zeros([y x]);
medoidContainer = zeros([y*x, length(imageList), 3]);
% loop through all images in the resizeFolder
for i=1:length(imageList)
% get filename and load image and alpha channel
fname = imageList(i).name;
[container, ~, alpha] = imread([resizeFolder fname]);
% convert alpha channel to zeros and ones, add to alphaImage
alphaImage = alphaImage + (double(alpha) / 255);
% add (r,g,b) values to medoidContainer and reshape to single line
medoidContainer(:, i, :) = reshape(im2double(container), [y*x 3]);
end
% loop through every pixel
for i=1:(y * x)
% convert i to coordinates for alphaImage
[xCoord, yCoord] = ind2sub([x y],i);
if alphaImage(yCoord, xCoord) == 0
% write default value to medoidImage if alpha is zero
medoidImage(1, i, 1:3) = 0;
else
% calculate distances between all values for current pixel
distances = pdist(squeeze(medoidContainer(i,:,1:3)));
% convert found distances to matrix of distances
distanceMatrix = squareform(distances);
% find index of image with the medoid value
[~, j] = min(mean(distanceMatrix,2));
% write found medoid value to medoidImage
medoidImage(1, i, 1:3) = medoidContainer(i, j, 1:3);
end
end
% replace values larger than one (in alpha channel) by one
alphaImage(alphaImage > 1) = 1;
% reshape image to original proportions
medoidImage = reshape(medoidImage, y, x, 3);
% save medoid image
imwrite(medoidImage, [outputFolder 'medoid_modified.png'], 'Alpha', alphaImage);
end
I didn't include the whole code, just this function (for brevity's sake), if anyone needs more (for a better understanding of it), please let me know and i'll include it.
When you call ind2sub, you give the size [x y], but the actual size of alphaImage is [y x] so you are not indexing the correct location with xCoord and yCoord.
how do i convert an image represented as double into an image that i can use to produce a histogram?
(with dohist:)
% computes the histogram of a given image into num bins.
% values less than 0 go into bin 1, values bigger than 255
% go into bin 255
% if show=0, then do not show. Otherwise show in figure(show)
function thehist = dohist(theimage,show)
% set up bin edges for histogram
edges = zeros(256,1);
for i = 1 : 256;
edges(i) = i-1;
end
[R,C] = size(theimage);
imagevec = reshape(theimage,1,R*C); % turn image into long array
thehist = histc(imagevec,edges)'; % do histogram
if show > 0
figure(show)
clf
pause(0.1)
plot(thehist)
axis([0, 256, 0, 1.1*max(thehist)])
end
I am guessing that you just need to normalize your image first, to do this you can use:
255*(theimage./(max(theimage(:)));
Your code seems fine, you could make sure the bounds get treated correctly with:
theimage(theimage<0) = 0;
theimage(theimage>255) = 255;
But this shouldnt be necessary, usually you either get a double image ranging [0,1] or uint8 [0,255] when you read an image with imread(). Just rescale to [0,255] in this case if needed.
Some other tips:
You can make the edges-vector like this:
edges = 0:255;
And theimage(:) is the same as reshape(theimage,1,R*C) in this case since you want one long vector.
The built-in function hist can be applied directly to images of class double.
Matlab documentation link
If you have an image which you suspect to have N bits of resolution on the interval [A,B], you can call hist directly on the image (without conversion) like:
[H,bins] = hist(IM,linspace(A,B,2^N));
to retrieve the histogram and bins or
hist(IM,linspace(A,B,2^N));
to simply plot the histogram.