Convert a 2D scatter plot into intensity image

7 vues (au cours des 30 derniers jours)
Ignacio Hernandez Montilla
Hi all,
First of all, apologies in case this question has already been asked. I spent last week trying to find a solution on my own, and reading the documentation, but I can't.
I must say that the idea that I currently use works fine, but as I plan to include figures in my dissertation, I'd like them to look more 'professional'.
This is what this piece of code does:
  • I load a grayscale image (in this case, a 2D mammography).
  • I compute the gradients using a Sobel filter.
  • In order to plot gradient vs pixel intensity (0-255), I reshape the original grayscale image and the gradient image (the magnitude image). Now each image has been reshaped as a 1xN vector, where N is the total number of pixels in the images.
  • I create a scatter plot with both vectors.
im = imread('Images\2Dmammogram.jpg');
im_gray = rgb2gray(im);
% To reduce computation time, I'll scale the image
im_gray = imresize(im_gray,0.25);
figure
imshow(im_gray)
title('Original image')
[mag,~] = imgradient(im_gray,'sobel');
% Plot gradient magnitude against image intensity values
% I will use a scatter plot
ints = reshape(im_gray,1,[]);
magt = reshape(mag,1,[]);
figure
scatter(ints, magt,'w.')
set(gca,'color',[0 0 0])
title('Gradient of image w/o Gaussian noise')
With this code, the scatter plot seems 'fine':
matlabquestion.PNG
However, I would like this to look smoother, and add some grayscale to the image, instead of only having 0s and 1s. I tried Kernel Density Esstimation (kdensity), but either I don't fully understand it or it doesnt really help in this situation:
% Just to begin with, I'll use the original gradient image: no motion and
% no Gaussian noise.
% One column per variate (pixel intensity and gradient magnitude)
X = [double(ints)', double(magt)'];
gridx1 = 0:1:255;
gridx2 = 0:2:ceil(max(magt))+1;
[x1, x2] = meshgrid(gridx1,gridx2);
x1 = x1(:);
x2 = x2(:);
xi = [x1(:) x2(:)];
% 2.2) Estimate the bandwidth (this might not be necessary for bivariate
% data (need to check documentation better?)
% Number of dimensions(?)
% d = 2;
% n = size(magt,2);
% I just created this following MATLAB documentation
% sigma1 = std(double(ints))*(4/((d+2)*n))^(1/(d+4));
% sigma2 = std(double(magt))*(4/((d+2)*n))^(1/(d+4));
% bw = [sigma1 sigma2];
disp('** Running Kernel Density Estimator **');
tic
figure
ksdensity(X,xi);
%mvksdensity(X,xi,'Bandwidth',bw);
title('Bivariate mammography data')
toc
matlabquestion2.PNG
I will gladly accept any idea or recommendation. Am I doing KDE wrong?
Many thanks,
Ignacio
  1 commentaire
Ignacio Hernandez Montilla
Hi it's me again.
I discovered that my KDE looked that bad becase my X distribution included too many (0,0) value pairs. That's why it showed that spike near (0,0). So I just had to remove them
X = [double(ints)', double(magt)'];
X( ~any(X,2), : ) = [];
And now it looks much better!
matlabquestion3.PNG
However, is there any way to see the hole surface 'from above', i.e. not caring about the 3rd dimension?
Thanks!

Connectez-vous pour commenter.

Réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by