Matlab code to generate images from entropy - matlab

Could you please help me with this question:
Assume that on average in binary images, 75% of the pixels are white, and 25% are black. What is the entropy of this source? Model this source in Matlab and generate some sample images according to this process

To find the entropy, you just need to apply the definition:
H = -0.25 * log2(0.25) - 0.75 * log2(0.75)
Since we are using log2, the result will be in bits.
As for generating a Matlab B&W (i.e. binary) image of size 512x512, you can simply do:
im = rand(512) < 0.75;
By convention, true = 1 = white and false = 0 = black.

Related

How to "disturb" an image with noise, but keep the orginal undisturbed pixel intact, and write to file in imagesc

For simple explanation of the title, imagine you're using a Photoshop layers, where noise is on the top layer.
% load a test image
I = rgb2gray(imread('peppers.png'));
% recreate image
cmap = colormap(); % grab current colormap
ncolors = size(cmap,1);
% do what imagesc does
Iind = double(I) - double(min(I(:)));
Iind = Iind / max(Iind(:));
% quantize image
Iind = round(Iind * ncolors + 0.5);
Iind(Iind > ncolors) = ncolors;
Iind(Iind < 1) = 1;
% convert to RGB from indexed image using cmap as palette
Irgb = ind2rgb(Iind,cmap);
imwrite(Irgb, 'filename.bmp');
These code will scale the colormap and write to file. However, by adding a artificial noise at a certain pixel location after loaded the image:
I(50,50) = rand(1);
This will generate a completely different colormap visually, and the one with the noise will look a little washed out. Top image is the original and bottom is with noise.
EDIT: The below image is the cropped image on the top left corner. Left is original and right is with noise. On the right you can see the color washed out a little bit, and if you look closely you can see a noise (dark blue color, position (50,50)).
Any idea how to still maintain the original after noise was added? Thanks in advance!
The issue is that you're adding noise (between 0 and 1) to the original image which has min = 8 and max = 255. This is reducing the new min to either 0 or 1 (the input image is of type uint8, and it seems MATLAB rounds the random number) which stretches out the colourmap slightly. There are two options to avoid this.
Add random noise in the range of the original image
I(50,50) = randi([min(I(:)), max(I(:))]);
Add random noise after scaling the original image
Irgb(50,50) = rand(1);

How to perform an orthographic projection on a z-Buffer image in Matlab?

I am facing the same problem as mentioned in this post, however, I am not facing it with OpenGL, but simply with MATLAB. Depth as distance to camera plane in GLSL
I have a depth image rendered from the Z-Buffer from 3ds max. I was not able to get an orthographic representation of the z-buffer. For a better understanding, I will use the same sketch as made by the previous post:
* |--*
/ |
/ |
C-----* C-----*
\ |
\ |
* |--*
The 3 asterisks are pixels and the C is the camera. The lines from the
asterisks are the "depth". In the first case, I get the distance from the pixel to the camera. In the second, I wish to get the distance from each pixel to the plane.
The settins of my camera are the following:
WIDTH = 512;
HEIGHT = 424;
FOV = 89.971;
aspect_ratio = WIDTH/HEIGHT;
%clipping planes
near = 500;
far = 5000;
I calulate the frustum settings like the following:
%calculate frustums settings
top = tan((FOV/2)*5000)
bottom = -top
right = top*aspect_ratio
left = -top*aspect_ratio
And set the projection matrix like this:
%Generate matrix
O_p = [2/(right-left) 0 0 -((right+left)/(right-left)); ...
0 2/(top-bottom) 0 -((top+bottom)/(top-bottom));...
0 0 -2/(far-near) -(far+near)/(far-near);...
0 0 0 1];
After this I read in the depth image, which was saved as a 48 bit RGB- image, where each channel is the same, thus only one channel has to be used.
%Read in image
img = imread('KinectImage.png');
%Throw away, except one channel (all hold the same information)
c1 = img(:,:,1);
The pixel values have to be inverted, since the closer the values are to the camera, the brigher they were. If a pixel is 0 (no object to render available) it is set to 2^16, so , that after the bit complementation, the value is still 0.
%Inverse bits that are not zero, so that the z-image has the correct values
c1(c1 == 0) = 2^16
c1_cmp = bitcmp(c1);
To apply the matrix, to each z-Buffer value, I lay out the vector one dimensional and build up a vector like this [0 0 z 1] , over every element.
c1_cmp1d = squeeze(reshape(c1_cmp,[512*424,1]));
converted = double([zeros(WIDTH*HEIGHT,1) zeros(WIDTH*HEIGHT,1) c1_cmp1d zeros(WIDTH*HEIGHT,1)]) * double(O_p);
After that, I pick out the 4th element of the result vector and reshape it to a image
img_con = converted(:,4);
img_con = reshape(img_con,[424,512]);
However, the effect, that the Z-Buffer is not orthographic is still there, so did I get sth wrong? Is my calculation flawed ? Or did I make mistake here?
Depth Image coming from 3ds max
After the computation (the white is still "0" , but the color axis has changed)
It would be great to achieve this with 3ds max, which would resolve this issue, however I was not able to find this setting for the z-buffer. Thus, I want to solve this using Matlab.

How to find maximum pixel intensity of the two regions obtained after finding the threshold of the image

We are working on DIP project where we found out the threshold of the gray scale image. Now we have to * find the maximum intensity of the two regions that we got, one region whose pixels are less than the threshold and the other whose pixels are greater than it. *
PS. We are not converting the image into binary image after finding the threshold. We just have to separate pixels in two regions and find the maximum intensity in each region
PS: We are working on MATLAB
Actually it is pretty simple to do.
Here is a function to do so:
function [ lowThrMaxIntns, highThrMaxIntns ] = FindMaxIntnesity ( mInputImage, thrLevel )
% Pixels which are lower than the threshold level
mLowThrImage = zeros(size(mInputImage));
% Pixels which are higher than the threshold level
mHightThrImage = zeros(size(mInputImage));
mLowThrImage(mInputImage < thrLevel) = mInputImage(mInputImage < thrLevel);
mHightThrImage(mInputImage >= thrLevel) = mInputImage(mInputImage >= thrLevel);
[lowThrMaxIntns lowThrMaxIntnsIdx] = max(mLowThrImage(:));
[highThrMaxIntns highThrMaxIntnsIdx] = max(mHightThrImage(:));
end
The output are only the intensities of the pixels.
If you need the pixel location, use the Idx variables (Use ind2sub for the sub scripts form).

maximum intensity projection matlab with color

Hi all I have a stack of images of fluorescent labeled particles that are moving through time. The imagestack is gray scaled.
I computed a maximum intensity projection by taking the maximum of the image stack in the 3rd dimension.
Example:
ImageStack(x,y,N) where N = 31 image frames.
2DProjection = max(ImageStack,[],3)
Now, since the 2D projection image is black and white, I was hoping to assign a color gradient so that I can get a sense of the flow of particles through time. Is there a way that I can overlay this image with color, so that I will know where a particle started, and where it ended up?
Thanks!
You could use the second output of max to get which frame the particular maximum came from. max returns an index matrix which indicates the index of each maximal value, which in your case will be the particular frame in which it occurred. If you use this with the imagesc function, you will be able to plot how the particles move with time. For instance:
ImageStack(x,y,N) where N = 31 image frames.
[2DProjection,FrameInfo] = max(ImageStack,[],3);
imagesc(FrameInfo);
set(gca,'ydir','normal'); % Otherwise the y-axis would be flipped
You can sum up bright pixels of each image with one another after coloring each image. This way you will have mixed colors on overlapped areas which you will miss using max function. Although I like the previous answer more than mine.
hStep = 1/N;
currentH = 0;
resultImage = uint8(zeros(x,y,3));
for i = 1 : N
rgbColor = hsv2rgb(currentH,1,0.5);
resultImage(:,:,1) = resultImage(:,:,1) + im(:,:,i) * rgbColor(1);
resultImage(:,:,2) = resultImage(:,:,2) + im(:,:,i) * rgbColor(2);
resultImage(:,:,3) = resultImage(:,:,3) + im(:,:,i) * rgbColor(3);
currentH = currentH + hStep;
end

Using rectangle in Matlab. Using Sum()

I have performed rgb2gray on an image and did a sobel edge detection on the image.
then did
faceEdges = faceNoNoise(:,:) > 50; %binary threshold
so it sets the outline of the image (a picture of a face), to black and white. Values 1 is white pixel, and 0 is black pixel. Someone said I could use this,
mouthsquare = rectangle('position',[recX-mouthBoxBuffer, recY-mouthBoxBuffer, recXDiff*2+mouthBoxBuffer/2, recYDiff*2+mouthBoxBuffer/2],... % see the change in coordinates
'edgecolor','r');
numWhite = sum(sum(mouthsquare));
He said to use two sum()'s because it gets the columns and rows of the contained pixels within the rectangle. numWhite always returns 178 and some decimal numbers.
If you have a 2D matrix M (this being -- for exmple -- an image), the way to count how many elements have the value 1 is:
count_1 = sum(M(:)==1)
or
count_1 = sum(reshape(M,1,[])==1)
If the target values are not exactly 1, but have a Δ-threshold of, let's say, +/- 0.02, then one should ask for:
count_1_pm02 = sum((M(:)>=0.98) & (M(:)<=1.02))
or the equivalent using reshape.