Given an image , how to efficiently (vectorization) make all the elements to zero except the one with information?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
The image is scaled color version of absolute value of the matrix.
Accepted Answer
Image Analyst
on 28 Aug 2018
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?
D_coder
on 28 Aug 2018
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?
D_coder
on 28 Aug 2018
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);
D_coder
on 29 Aug 2018
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.
D_coder
on 29 Aug 2018
got it thanks
More Answers (1)
Image Analyst
on 26 Aug 2018
Probably thresholding and masking.
mask = theImage ~= backgroundValue; % Threshold.
theImage(mask) = 0; % Mask
Attach your data in a .mat file if you want more help.
Categories
Find more on Image Preview and Device Configuration in Help Center and File Exchange
Products
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)