I was trying to binarize some images. In some images I get the pattern as it is but in some images I lose some of the pattern. I am using greythresh for binarizing. Is there any other method to improve the output.
I = imread('image.jpg');
I = rgb2gray(I);
I = uint8(255*mat2gray(I));
figure,imshow(I);
I=imresize(I,[128 128]);
figure,imshow(I);
I = medfilt2(I,[5 5]);
I1 = medfilt2(I,[5 5]);
I = adapthisteq(I1,'clipLimit',0.4,'Distribution','rayleigh');
figure,imshow(I);
level = graythresh(I);
BW = im2bw(I, level);
figure,imshow(BW);
input
output
Getting a unique threshold for the whole image seems to be bad in your case. You should try to perform adaptive local thresholding to better adapt to smooth image intensity variations.
You can find a matlab example here
Related
I'm supposed to add another image next to my threshold image with its original color like so: expected image
But I'm unsure how to do it having only achieving the binary image threshold on matlab. How do I show images side by side?
my result
clear all;
close all;
clc;
% read image
palm = imread('palmDown (2).jpg');
%split into RGB
redPalm = palm(:,:,1);
greenPalm = palm(:,:,2);
bluePalm = palm(:,:,3);
redLevel = -0.1;
greenLevel = -0.1;
blueLevel = 0.06;
redThresh = imbinarize(redPalm, redLevel);
greenThresh = imbinarize(greenPalm, greenLevel);
blueThresh = imbinarize(bluePalm, blueLevel);
colorSum = (redThresh&greenThresh&blueThresh);
colorSum2 = imcomplement(colorSum);
thumbFilled = imfill(colorSum2, 'holes');
figure;
imshow(thumbFilled); title('Sum of all');
There are many ways to colorize the thresholded image. One simple way is by multiplication:
palm = im2double(palm); % it’s easier to work with doubles in MATLAB
palm2 = palm * thumbFilled;
imshow([palm, palm2])
The multiplication uses implicit Singleton expansion. If you have an older version of MATLAB it won’t work, you’ll have to use bsxfun instead.
I have an RGB image obtained from saving the imagesc function as shown below. how to refine/smoothen the edges present in the image.
It consists of sharper edges, where I need to smoothen them. Im not able to find a solution for performing this for an RGB image. Instead of the staircase effect seen in the image I'd like to even out the edges. Please help thanks in advance.
maybe imresize will help you:
% here im just generating an image similar to yours
A = zeros(20);
for ii = -2:2
A = A + (ii + 3)*diag(ones(20-abs(ii),1),ii);
end
A([1:5 16:20],:) = 0;A(:,[1:5 16:20]) = 0;
subplot(121);
imagesc(A);
title('original')
% resizing image with bi-linear interpolation
B = imresize(A,100,'bilinear');
subplot(122);
imagesc(B);
title('resized')
EDIT
here I do resize + filtering + rounding:
% generates image
A = zeros(20);
for ii = -2:2
A = A + (ii + 3)*diag(ones(20-abs(ii),1),ii);
end
A([1:5 16:20],:) = 0;A(:,[1:5 16:20]) = 0;
subplot(121);
imagesc(A);
title('original')
% resizing
B = imresize(A,20,'nearest');
% filtering & rounding
C = ceil(imgaussfilt(B,8));
subplot(122);
imagesc(C);
title('resized')
solution
use imfilter and fspecial to perform a convolution of you image with gaussian.
I = imread('im.png');
H = fspecial('gaussian',5,5);
I2 = imfilter(I,H);
change 'blurlevel' parameter (which determines the gaussian kernel size) to make the image smoother or sharper.
result
If you are just looking for straighter edges, like an elevation map you can try contourf.
cmap = colormap();
[col,row] = meshgrid(1:size(img,2), 1:size(img,1));
v = linspace(min(img(:)),max(img(:)),size(cmap,1));
contourf(col,row,img,v,'edgecolor','none');
axis('ij');
This produces the following result using a test function that I generated.
New to MATLAB and image processing.I need to know how to segment an image into foreground and background, then generate a binary image as output.
I need this as an output:
I have already tried to accomplish this with online tutorials and this is what i managed to get:
Its a good start but not exactly what i need.
My Code:
I = imread('AssignmentInput.jpg');
figure;
imshow(I);
title('Step-1: Load input image');
img_filtered = I;
for c = 1 : 3
img_filtered(:, :, c) = medfilt2(I(:, :, c), [3, 3]);
end
figure;
imshow(img_filtered);
title('Step-3:Noise Removal');
H = fspecial('gaussian'); % Create the filter kernel.
img_filtered = imfilter(img_filtered,H); % Blur the image.
Mask = im2bw(img_filtered, 0.9); % Now we are generating the binary mask.
img_filtered([Mask, Mask, Mask]) = 0; % Now we have the image.
figure;
imshow(img_filtered);
title('Step-5:Segmented Image');
For a better noise removal process and cleaner separation between foreground and background, you can also add morphological operations like:
se = strel('square',2);
I = imclose(I,se);
You can try out different variations of 'strel' class also. The image below is after imclose operation with a square of 2 pixel
I have a noisy image that I am trying to clean using a lowpass filter (code below, modified from here). The image I get as a result is essentially identical to the one I gave as an input.
I'm not an expert, but my conclusion would be that the input image is so noisy that no patterns are found. Do you agree? Do you have any suggestion on how to interpret the result?
Result from the code:
Input image:
Code:
clear; close all;
frame = 20;
size_y = 512; % This is actually size_x
size_x = 256; % This is actually size_y
roi=5;thresh=100000;
AA = imread('image.png');
A = zeros(size_x, size_y);
A = AA(1:size_x, 1:size_y);
A(isnan(A)) = 0 ;
B = fftshift(fft2(A));
fabs = abs(B);
figure; imshow(B);
local_extr = ordfilt2(fabs, roi^2, ones(roi)); % find local maximum within 3*3 range
result = (fabs == local_extr) & (fabs > thresh);
[r, c] = find(result);
for i=1:length(r)
if (r(i)-128)^2+(c(i)-128)^2>thresh % periodic noise locates in the position outside the 20-pixel-radius circle
B(r(i)-2:r(i)+2,c(i)-2:c(i)+2)=0; % zero the frequency components
end
end
Inew=ifft2(fftshift(B));
figure;
subplot(2,1,1); imagesc(A), colormap(gray); title('Original image');
subplot(2,1,2);imagesc(real(Inew)),colormap(gray); title('Filtered image');
For filtering this kind of signal, you can try to use the median filter. It might be more appropriated than a means or Gaussian filter. The median filter is very effective on "salt and paper" noise when the mean just blur the noise.
As the signal seems very noisy, you need to try to find the good size of kernel for the filter. You can also try to increase the contrast of the image (after filtering) in order to see more the difference between the gray levels.
I want to use mathematical morphology function in MATLAB to find the boundary of can.png image. The input image is:
I want to get a boundary such as :
I tried to use different combination and parameter using strel, imerode, imdilate , But the result is not good enough (far from the expectation)
One of my trial code is:
a = imread ('can.png');
b = im2bw(a);
SE = strel('rectangle', [10 50 ]) ;
i2 = imdilate(b,SE);
figure(1); imshow(i2);
p = ones(4);
c = b - imerode(b,p);
figure(2); imshow(c);
The output is:
Can any body help me, how to create the expected image (black background with thin boundary for the can, please? Thank you very much.
Binarize on its morphological gradient, then do a dilation with an elementary SE, fill holes and finally obtain its border (trivial given the current image). This doesn't require any magical arbitrary threshold.
im = imread('can.png');
% turn image to BW
imb = (im < 220);
% fill the holes in the image
imf = imfill(imb, 'holes');
% find the edge
ed = edge(imf);
Resulting image: