How can I linearly index a masked image - matlab

I have an image which i applied a mask to
Input_image = double(imread('re1.jpg')); % original image
mask = double(imread(/block_text.bmp')); % text mask
mask = mask ./ max(mask(:));
X = Input_image.*mask; % partially observed image. (matrix with missing elements)
This code gives this image
EDIT: what i mean is that i have the code below and i want to modify to use images in the code above instead of random data.
i.e matrix M becomes my image X. but what i dont know is how to get omega in my case.
m=400, n=500;
M = rand(m,r) * rand(r,n); % randomly generated matrix
I = randi([0 1],m,n);
Omega = find(I);
A = M(Omega);

Related

Matlab image translation matrix

I'm very new to Matlab. I'm learning some image manipulation basics, and I'm a bit confused on how to write a translation without using imtranslate.
this is my code but it just displays a black background. Thank you.
img = imread('name2.png');
figure(1);
% pixel matrix
[orig_x, orig_y,z] = size(img);
final_x = 600;
final_y = 600;
% define the final array with calculated dimensions and fill the array with zeros ie.,black
final_img = uint8(zeros([final_x final_y 3 ]));
for i = 1 : size(final_img, 1)
for j = 1 : size(final_img, 2)
new_x = img(i) + 5;
new_y = img(j) + 5;
% fprintf('X: %f\n',new_x); % prints 255
final_img(i) = new_x;
final_img(j) = new_y;
end
end
imshow(final_img);
This is one solution for 'translation only' transformation.
I = imread('Lenna.png');
shiftX = 5; % shift columns
shiftY = 5; % shift rows
% Assigning empty matrix for result, expected to be shiftX-1 larger in rows and shiftY-1 larger in columns
nI = uint8( zeros(size(I,1)+shiftY-1, size(I,2)+shiftX-1, size(I,3));
% Translate
nI(shiftY:end, shiftX:end, :) = I;
imshow(nI)
Now the image will start from (x,y) = (5,5) instead of (1,1). Also note that in matlab image coordinate system, x and y axis start from upper left corner (documentation).
final_img:
You've defined "final_img" with new x and new y but you haven't replaced the zeros in the red/green/blue values. It's all black because of your initialisation filling the final_img with all zeros.
Maybe try this instead of what you've written:
%{
[X,map] = imread('name2.png');
figure(1);
% X should be 600 by 600
%Translate X however you wish, e.g.:
X = X +5;
%Verify that the colormap, map, is not empty, and convert
%the data in X to RGB and store as your final_img.
if ~isempty(map)
final_img = ind2rgb(X,map);
end
%}
I am also not sure if you want to be indexing img with just a single i without the the other dimensions like you have:
new_x = img(i) + 5;
?
For the problems in your specific code, I wrote in the comments some of them.
A short way to achieve image translation is by 2D convolution with a filter of zeros and just one 1, that will preserve the values of the image, but relocate them according to the size of the filter and the position of the 1 in it.
That seems you want to move the image but preserve the size of the total image, if I get it right. So:
r=3; c=5; % number of rows and columns to move
filt=zeros(r*2+1, c*2+1); filt(end)=1; % the filetr
img2=conv2(img,filt,'same'); % the translated image
Just for the example, lets translate "cameraman" with 20 rows and columns:
img=imread('cameraman.tif');
imshow(img)
r=20; c=20;
filt=zeros(r*2+1, c*2+1); filt(end)=1;
img2=conv2(img,filt,'same');
figure; imshow(img2,[])

Creat a circular ROI and account for partial pixel values

I have a image of low resolution, 128x128 pixels. I need to obtain the mean value of a circle ROI, in order to do that I use the easy method:
%% Draw circle ROI
t = 0:pi/500:2*pi;
xi = ((R0/pixelSize)*cos(t)+63.5+x0+((Rsphere)/pixelSize)*cos(theta))*4;
yi = ((R0/pixelSize)*sin(t)+63.5+y0+((Rsphere)/pixelSize)*sin(theta))*4;
%% Calculate roi statistics
line(xi,yi,'LineWidth',1,'Color',color);
ROImask = poly2mask(xi,yi, size(im,1),size(im,2));
ptROI = find(ROImask);
ROImean = mean(im(ptROI));
The problem here is that using this method I don't account for the partial value of a pixel in the ROI, as can be seen in the image.
Is there any direct way to obtain the mean of the ROI weighting the value of the pixels?
Thanks
If you really want to do this exactly, you'll need to do some calculus (integral of a circle on a square domain for each pixel). But, this probably overkill for your application. My suggestion is to calculate your circle on a fine grid, then resize it to match the image:
upFactor = 3;
% load built-in example image
x = imread('rice.png');
% convert to double
x = im2double(x);
% define the ROI
center = [68.5, 180]; % [row, column]
radius = 1; % pixels
% do the distance calculation
% (getting the coordinate systems to match is the hardest part, try making
% small examples to see how it works)
iVector = (0:size(x,1)*upFactor-1)/upFactor + .5 + 1/upFactor/2;
jVector = (0:size(x,2)*upFactor-1)/upFactor + .5 + 1/upFactor/2;
[I, J] = ndgrid( iVector - center(1), jVector - center(2));
sqDist = I.^2 + J.^2;
insideBig = double(sqDist <= radius^2); % need this to not be logical type
% this resizes back to the original image size my taking the mean of each
% upFactor by upFactor block
inside = reshape( mean( im2col( insideBig, [upFactor upFactor], 'distinct')), size(x));
% check that we have the values we expect
uniqueVals = unique(inside(:))
% show examples
figure
imagesc(x)
figure
imagesc(inside)
result = sum(sum( x .* inside )) / sum(inside(:))

Convert Matlab code into Simulink

I would like to convert a image processing program(part of the program below) from Matlab to Simulink and possibly convert the simulink diagram into C code later on. I have 0 experience in Simulink and was wondering if there's any limitations on the types of matlab program/functions that can be converted and how I would go about doing this. Thanks.
clear all
clc
% Read in an image 1
C1 = imread('cloud1.jpg');
Cloud1 = C1(:,:,1); % use only one color
%Cloud1 = Cloud1'; % transpose to get (x,y) instead of (y,x)
Cloud1_xsize = size(Cloud1,2); % get x size of image
Cloud1_ysize = size(Cloud1,1); % get y size of image
%figure(3), imshow(Cloud1) % to plot you need to transpose back to their coordinate system
%hold on
% Read in an image 2
C2 = imread('cloud2.jpg');
Cloud2 = C2(:,:,1); % use only one color
%Cloud2 = Cloud2'; % transpose to get (x,y) instead of (y,x)
Cloud2_xsize = size(Cloud2,2); % get x size of image
Cloud2_ysize = size(Cloud2,1); % get y size of image
%figure(2), imshow(Cloud2)
%hold on
% show the shift in the initial images several times
num = 0;
for k = 1:4
num=num+1;
pause(.5)
figure(1), h1=imshow(C1)
xlabel('FIGURE 1')
F(num) = getframe(gcf);
%image(F.cdata)
%colormap(F.colormap)
pause(0.25)
figure(1), h2=imshow(C2)
xlabel('FIGURE 2')
num=num+1;
F(num) = getframe(gcf);
%image(F.cdata)
%colormap(F.colormap)
end
% Play the movie twenty times
%movie(F,20)
%%%% Set the template size %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% First calc the number of pixels in the shortest direction of the image (usually y direction)
MinSize = min(Cloud1_xsize, Cloud1_ysize); % number of pixels in shortest direction
%%% N is the minimum number of boxes in the shorter direction (usually y direction).
%%% In the shorter axis (usually y)there will be N-2 boxes analyzed.
%%% This is because the top and bottom boxes are considered too close to the edge to use.
%%% In the larger direction (usually x) there may be more boxes.
N = 6;
EdgeBoxSize = 1; % the number of edge boxes along each edge
TempWidth = floor(MinSize / N); % the pixel width of each template box
TempHeight = TempWidth; % make the template height and width the same size so corr part works good
%%% Now calculate the exact number of boxes in x and y directions
%%% This depends on the number of x versus y pixels.
Nx = floor(Cloud1_xsize/TempWidth);
Ny = floor(Cloud1_ysize/TempWidth);

How to obtain the Hadamard Transform of an image in matlab

I have a 128x128 grascale image that i wish to find the Hadamard transform of with Normal Hadamard, sequency, and dyadic ordering.
imdata = imread('origim.png'); %Load image
new = rgb2gray(imdata); %Convert to 2D Grayscale
N = 128;
H = hadamard(N); % Hadamard matrix
y = fwht(new,N,'sequency') %Perform Fast-walsh-hadamard-transform with order 128
imshow(y); %Display image transform
I may be doing it wrong, however y should be the transformed image if i understand the matlab walch transform correctly. When i try runing it i get an error with y = fwht(new,N,'sequency')
Before processing convert the image into double. Then put semicolon (;) at the end of y = fwht(new,N,'sequency'). Then you will get transformed image.
Just try the below code.
imdata = imread('peppers.png'); %Load image
new = rgb2gray(imdata); %Convert to 2D Grayscale
neww = im2double(new);
N = 128;
H = hadamard(N); % Hadamard matrix
y = fwht(neww,N,'sequency'); %Perform Fast-walsh-hadamard-transform with order 128
imshow(y); %Display image transform

Converting code to take RGB image instead of grayscale

I have this code converting a fisheye image into rectangular form but the code is only able to perform this operation on a grayscale image. Can anybody help converting the code to perform the operation on a RGB image. The code is as follows:
edit: I have updated the code to contain a functionality which performs interpolation in each color channel. But this seem to disform the output image. See pictures below
function imP = FISHCOLOR (imR)
rMin=0.1;
rMax=1;
[Mr, Nr, Dr] = size(imR); % size of rectangular image
xRc = (Mr+1)/2; % co-ordinates of the center of the image
yRc = (Nr+1)/2;
sx = (Mr-1)/2; % scale factors
sy = (Nr-1)/2;
M=size(imR,1);N=size(imR,2);
dr = (rMax - rMin)/(M-1);
dth = 2*pi/N;
r=rMin:dr:rMin+(M-1)*dr;
th=(0:dth:(N-1)*dth)';
[r,th]=meshgrid(r,th);
x=r.*cos(th);
y=r.*sin(th);
xR = x*sx + xRc;
yR = y*sy + yRc;
imP =zeros(M, N); % initialize the final matrix
for k=1:3 % colors
T = imR(:,:,k);
Ichannel = interp2(T,xR,yR);
imP(:,:,k)= Ichannel; % add k channel
end
SOLVED
Input image <- Image link
Grayscale output, what i would like in color <- Image link
Try changing these three lines:
[Mr Nr] = size(imR); % size of rectangular image
...
imP = zeros(M, N);
...
imP = interp2(imR, xR, yR); %interpolate (imR, xR, yR);
...to these:
[Mr Nr Pr] = size(imR); % size of rectangular image
...
imP = zeros(M, N, Pr);
...
for dim = 1:Pr
imP(:,:,dim) = interp2(imR(:,:,dim), xR, yR); %interpolate (imR, xR, yR);
end