i have an image descriptor in a file.modQDH i need to display this image
this is my code
imagefiles = dir('*.modQDH');
nfiles = length(imagefiles); % Number of files found
fprintf('number est ');
fprintf('%d ',nfiles );
fprintf('%n');
A = importdata('obj1__0.modQDH');
disp(A);
when i use disp(a) the result is my vetcor
but me i need the image corresponding to this vector
i have used image(A) and imshow (A) but it doesn't works
disp shows the value of a variable, it does not create an image rendering
see
http://www.mathworks.com/help/matlab/ref/disp.html
try using imagesc which attempts to take care of some of the scaling issues
Related
As the title says, the output I’m getting out of this function is incorrect. By incorrect I mean that the data are overflowing. How do I normalise the matrix correctly? Currently almost all of the pictures I get are white.
I called the function from another MATLAB file like this:
mask = [3,10,3;0,0,0;-3,-10,-3];
A = imread(“football.jpg”);
B = ConvFun(A,mask);
function [ image ] = ConvFun( img,matrix )
[rows,cols] = size(img); %// Change
%// New - Create a padded matrix that is the same class as the input
new_img = zeros(rows+2,cols+2);
new_img = cast(new_img, class(img));
%// New - Place original image in padded result
new_img(2:end-1,2:end-1) = img;
%// Also create new output image the same size as the padded result
image = zeros(size(new_img));
image = cast(image, class(img));
for i=2:1:rows+1 %// Change
for j=2:1:cols+1 %// Change
value=0;
for g=-1:1:1
for l=-1:1:1
value=value+new_img(i+g,j+l)*matrix(g+2,l+2); %// Change
end
end
image(i,j)=value;
end
end
%// Change
%// Crop the image and remove the extra border pixels
image = image(2:end-1,2:end-1);
imshow(image)
end
In a convolution, if you want the pixel value to stay in the same range
, you need to make the mask add up to 1. Just divide the mask by sum(mask(:)) after defining it. This is however, not the case you are dealing with.
Sometimes that is not the needed. For example if you are doing edge detection (like the kernel you show), you don't really care about maintaining the pixel values. In those cases, the plotting of unnormalized images is more the problem. You can always set the imshow function to auto select display range: imshow(image,[]).
Also, I hope this is homework, as this is the absolutely worst way to code convolution. FFT based convolution is about 100 times faster generally, and MATLAB has an inbuilt for it.
I am trying to perform image analysis to get the boundary of an image. The problem is, there is an overlap of more than one point on the data. The problem occurs due to the pixel width of the boundary. ie, my algorithm reads the image, and because the boundary is thick, I get multiple ( sometimes 2 or 3) data points that represent the boundary .As you can see in the image, I have analysed an image and have the scatter plot showing the coordinates. The boundary has more than one element representing it. I need to reduce my vectors (of coordinates) such that a boundary is represented by only one line. I attach the code along with the question for your reference. I am doing this so as to get one single data that can represent the whole image.
clc;close all;
clear all;
folder = 'C:\Users\Adi\Desktop'; % image folder
baseFileName = 'part8.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
rgbImage = imread(fullFileName); % read the image
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
figure;
greenChannel = rgbImage(:, :, 2);
binaryImage = greenChannel < 200; % convert image to BW
imshow(binaryImage);
i=1;
j=1;
data=zeros(rows,columns);
n=1;% sample every n rows or col of the image
img2=zeros(rows/n,columns/n);
for col=1:n:columns %reducing the size of the image
for row=1:n:rows
if(binaryImage(row,col)==1)
% data(row,col)=1;
x(i)=row;
y(i)=col; % locate where the image is present
img2(x(i),y(i))=1;
i=i+1;
end
end
end
figure;
scatter(x,y);
figure;
imshow(img2);
blankImage=zeros(length(1:rows),length(1:columns));
m=1; % counter variable
for k=2:rows-1
for l=2:columns-1
if((binaryImage(k+1,l)==0)&&(binaryImage(k,l+1)==0)&&(binaryImage(k-1,l)==0)&&(binaryImage(k,l-1)==0)&&(binaryImage(k,l)==0))
% % if all surrounding pixels are black, ignore them
blankImage(k,l)=0;
elseif((binaryImage(k+1,l)==1)&&(binaryImage(k,l+1)==1)&&(binaryImage(k-1,l)==1)&&(binaryImage(k,l-1)==1)&&(binaryImage(k,l)==1))
%if all surrounding pix are white ,ignore them
blankImage(k,l)=0;
else
blankImage(k,l)=1; % get the boundary elements
x_brep(m)=k;
y_brep(m)=l;
m=m+1;
end
end
end
figure;
imshow(blankImage);
figure;
scatter(x_brep,y_brep);
Sample image and output:
If you have the image processing toolbox, you can take your original image, and use bwmorph - for example, thin or skel, to reduce it to a single pixel line. Note that you may need to invert your image (so that it is a white line on a black background).
Presuming your original image as shown is already binary and is stored in BW (but with line as black on white), it should be as simple as (~BW just inverts the image).
BW2 = bwmorph(~BW,'thin', Inf);
If your image is not already binary you will need to convert it to binary first.
How to do this on matlab?
zero pad the face image with a five‐pixel
thick rim around the borders of the
image. show the resulting image.
Must be manual codes on script.
save this function as create_padded_image.m
function padded_image = create_padded_image(image, padding)
if nargin < 2
% if no padding passed - define it.
padding = 5;
end
if nargin < 1
% let's create an image if none is given
image = rand(5, 4)
end
% what are the image dimensions?
image_size = size(image);
% allocate zero array of new padded image
padded_image = zeros(2*padding + image_size(1), 2*padding + image_size(2))
% write image into the center of padded image
padded_image(padding+1:padding+image_size(1), padding+1:padding+image_size(2)) = image;
end
Then call it like this:
% read in image - assuming that your image is a grayscale image
$ image = imread(filename);
$ padded_image = create_padded_image(image)
This sounds like homework, so I will just give you a hint:
In MATLAB it is very easy to put the content of one matrix into another at precisely the correct place. Check out the help for matrix indexing and you should be able to solve it.
I realize you want to code this yourself, but for reference, you can use the PADARRAY function. Example:
I = imread('coins.png');
II = padarray(I,[5 5],0,'both');
imshow(II)
Note this works also for multidimensional matrices (RGB images for example)
I have a binary image and need to convert all of the black pixels to white pixels and vice versa. Then I need to save the new image to a file. Is there a way to do this without simply looping over every pixel and flipping its value?
If you have a binary image binImage with just zeroes and ones, there are a number of simple ways to invert it:
binImage = ~binImage;
binImage = 1-binImage;
binImage = (binImage == 0);
Then just save the inverted image using the function IMWRITE.
You can use imcomplement matlab function. Say you have a binary image b then,
bc = imcomplement(b); % gives you the inverted version of b
b = imcomplement(bc); % returns it to the original b
imwrite(bc,'c:\...'); % to save the file in disk
In Matlab, by using not we can convert 1's into 0's and 0's into 1's.
inverted_binary_image = not(binary_image)
[filename, pathname] = uigetfile({'*.bmp'},'Text as image');
img=imread(filename);
img=im2double(img);
[r,c,ch]=size(img);
%imshow(img);
invert_img=img;
if(ch==1)
for i=1:r
for j=1:c
if(invert_img(i,j)==0)
invert_img(i,j)=1;
else
invert_img(i,j)=0;
end
end
end
end
How to draw a grid over an image. It should become part of that image itself.
It should be able to show some rows and columns over the image itself. The lines for rows and columns can be specified.
Actually I was encouraged by the way some research paper discusses about the results they have about image warping. One of the links is this: http://www.hammerhead.com/thad/morph.html
There are a number of related questions on SO that discuss ways to modify an image. Here are the two general approaches:
1. Modify the image data directly: I discuss this in my answer to this other SO question. Since image data can be 2-D or 3-D, you can use multidimensional indexing to modify the raw image data, creating lines along given rows and columns. Here's an example that changes every 10 rows and columns in the image to black:
img = imread('peppers.png'); %# Load a sample 3-D RGB image
img(10:10:end,:,:) = 0; %# Change every tenth row to black
img(:,10:10:end,:) = 0; %# Change every tenth column to black
imshow(img); %# Display the image
And now the image data in the variable img has black lines on it, and you can write it to a file or do whatever other processing you want to it.
2. Plot the image and the lines, then turn the axes/figure into a new image: The link to Steve Eddins' blog in zellus' answer shows an example of how you can plot an image and add lines to it. However, if you want to save or perform processing on the displayed image, you will have to save the displayed image as an image matrix. How you can do this has been discussed in these other SO questions:
How can I save an altered image in MATLAB?
Turn Matlab plot into image
Superimposing line plots on images from the blog 'Steve on Image Processing' has a nice example on overlaying a grid over an image.
Actually I watched this question after doing this code by my own .... the code reads an image and draw grid on the image every input parameter
I hope it would do any good :)
Watch the matlab code :
function [ imageMatdouble ] = GridPicture( PictureName , countForEachStep )
%This function grid the image into counts grid
pictureInfo = imfinfo(PictureName); %load information about the input
[inputImageMat, inputImageMap] = imread(PictureName); % Load the image
if (pictureInfo.ColorType~='truecolor')
warning('The function works only with RGB (TrueColor) picture');
return
end
%1. convert from trueColor(RGB) to intensity (grayscale)
imageMat = rgb2gray(inputImageMat);
%2. Convert image to double precision.
imageMatdouble =im2double(imageMat);
% zero is create indicated to black
height = pictureInfo.Height ;
width = pictureInfo.Width
i=1;j=1;
while (i<=height )
for j=1:width
imageMatdouble(i,j)=1;
end
j=1;
if (i==1)
i=i+countForEachStep-1;
else
i=i+countForEachStep;
end
end
i=1;j=1;
while (i<=width )
for j=1:height
imageMatdouble(j,i)=1;
end
j=1;
if (i==1)
i=i+countForEachStep-1;
else
i=i+countForEachStep;
end
end
imwrite(imageMatdouble,'C:\Users\Shahar\Documents\MATLAB\OutputPicture.jpg')
end