Given an image , how to efficiently (vectorization) make all the elements to zero except the one with information?

 Accepted Answer

This will do it:
clc; % Clear the command window.
clearvars;
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;fontSize = 22;
s = load('image_data.mat')
D = s.D;
realD = abs(real(D));
imagD = abs(imag(D));
subplot(2, 3, 1);
imshow(realD, []);
axis square
title('Real Image', 'FontSize', fontSize);
impixelinfo();
subplot(2, 3, 2);
histogram(realD);
grid on;
title('Histogram of Real Image', 'FontSize', fontSize);
subplot(2, 3, 4);
imshow(imagD, []);
axis square
title('Imaginary Image', 'FontSize', fontSize);
impixelinfo();
subplot(2, 3, 5);
histogram(imagD);
grid on;
title('Histogram of Imaginary Image', 'FontSize', fontSize);
backgroundValueR = 5e8;
backgroundValueI = 3e8;
binaryImageR = realD > backgroundValueR;
binaryImageI = imagD > backgroundValueI;
subplot(2, 3, 3);
imshow(binaryImageR, []);
axis square
impixelinfo();
title('Real Image, binarized', 'FontSize', fontSize);
subplot(2, 3, 6);
imshow(binaryImageI, []);
axis square
impixelinfo();
title('Imaginary Image, binarized', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Apply the mask
% Mask the image using bsxfun() function to multiply the mask by each channel individually.
maskedRealImage = bsxfun(@times, realD, cast(binaryImageR, 'like', realD));
maskedImagImage = bsxfun(@times, imagD, cast(binaryImageI, 'like', imagD));
figure;
subplot(2, 1, 1);
imshow(maskedRealImage, []);
axis square
impixelinfo();
title('Masked Real Image, binarized', 'FontSize', fontSize);
subplot(2, 1, 2);
imshow(maskedImagImage, []);
axis square
impixelinfo();
title('Masked Imaginary Image, binarized', 'FontSize', fontSize);

9 Comments

what does '.D' do? How did you select the background value for real part and imaginary part? Is there any automatic way of doing that? If the image is inside a loop and everytime it changes little by little I want to automatically adapt the mask by automatically selecting background value. How do I convert real and imaginary binarized images back to their original form. Since I need to multiply them with another matrix. Here is my pseudo code to make you understand better
My pseudo
if true
This is my pseudo code
for numberofiterations
maskedimage = maskedimage.*Anotherimage OR (realmasked +i*imaginarymasked).*Anotherimage
end
load() returns a structure, which I call s. The members/fields of the structure are the variable names you used. So I called D=s.D to take the "D" field of the "s" structure and put it into a simpler variable called D.
You can inspect the histogram and use that to find a threshold. It really depends on what your images look like in general. Like do they vary in intensity? Does that thing change its size dramatically?
is there any automatic way of selecting the threshold without any intervention.Since my image is inside the loop . How do I convert binarized image to the original form keeping the information and making everything else zero
What if you just take the peak of the histogram and run down from the peak until you hit a certain percentage of its height?
How do I convert binarized image to the original form keeping the information and making everything else zero
If you want a complex image again, but masked by the binary image, simply combine the real and imaginary masked images:
complexImage = complex(maskedRealImage, maskedImagImage);
backgroundValueR = 5e8; backgroundValueI = 3e8; HOW TO GET THESE AUTOMATICALLY WITHOUT INTERVENTION INSIDE A LOOP
I gave two comments above telling you how to do that. You inspect the histogram. It's a judgement call - there are lots of values you could use so just decide where in the histogram you want to threshold it.

Sign in to comment.

More Answers (1)

Probably thresholding and masking.
mask = theImage ~= backgroundValue; % Threshold.
theImage(mask) = 0; % Mask
Attach your data in a .mat file if you want more help.

Asked:

on 26 Aug 2018

Edited:

on 29 Aug 2018

Community Treasure Hunt

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

Start Hunting!