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
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 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
I have a binary image which is the segmented form of another color image .
As you know , a binary image is 2-d but an rgb image is 3-d , how can i multiply them together ?
i tried this code which resulted in a strange pic
function skinCrop(bwSkin,colorSkin)
for i = 1:size(colorSkin,1)
for j = 1:size(colorSkin,1)
if bwSkin(i,j) == 0
colorSkin(i,j,:) = 0;
end
end
end
imshow(colorSkin);
end
The original image was
The resulting image was :
I expected it to be a hand against a dark background , so why do the right part appear that way ?
You should avoid loops when not needed in matlab:
mask = cat(3,bwSkin,bwSkin,bwSkin);
output = mask.*colorSkin;
You might have to change the types in order for the multiplication to succeed:
output = uint8(mask).*colorSkin;
Or:
output = double(mask).*colorSkin;
You are using the wrong dimension length for your second dimension:
for j = 1:size(colorSkin,1)
should be
for j = 1:size(colorSkin,2)
A more efficient way to do the multiplication is
function mult = skinCrop( bwSkin, colorSkin )
%
% multiplying 3D color image with 2D mask
%
mult = nsxfun( #times, bwSkin, colorSkin );
% show the result
imshow( mult ); title('skin cropped image');
As #zenopy noted, you might need to cast your variable to double type.
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 need to rasterize an image in matlab.
I have a b/w image and want to chunk it up in 8x8 blocks and get a mean value from every block. Then I want to replace the block with a new block that is made up by ones and zeros, with a amount of ones depending on the mean value from the original block.
Thanks in advance!
This will get you started. It is the downsampled image where each value is between zero and the square of the block size. You are on your own expanding that integer into a sub matrix.
bs = 8
a = imread('trees.tif');
[r,c] = size(a);
d = imresize(a,[round(r/bs), round(c/bs)]);
figure(1)
imshow(a)
figure(2)
imshow(d)
mv = max(d(:))
d = round(double(d)/double(mv)*bs*bs);
figure(3)
imagesc(d)