Use imhistmatch() function on non-rectangular regions of images
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi all,
I have two images and I want to adjust the histogram of one of them (image1.tif) to match the histogram of a reference image (ref_image.tif). The tricky part is that I am only interesting in matching the histograms for particular (non-rectangular) regions of the images.
Quick description of the script: it loads the reference image and has you click on pixels along the edge of the area of interest to create a mask. The area outside of this is set to black, and inside is the original image data (see image1_roi & ref_image_roi (after masking) included in the zipped folder). Same is then done for the image whose histogram I want to adjust.
I am trying to use the imhistmatch function to do this, but it always includes the entire image, or a rectangular subset. I would like to make a custom imhistmatch function that excludes all pixel values that are less than some small value (0.01 for instance).
If possible I would like to use some sort of conditional statement in the inputs for imhistmatch, for example:
image1_histMatch = imhistmatch(image1_roi(image1_roi <0.01),ref_image_roi(ref_image_roi<0.01))
I know this is not the correct syntax to specify "only perform this function on pixels which have a value less than 0.01", but I'm not sure how to do it.
Your help is much appreciated!!
SCRIPT
chooseVertices = 1; % first time you run it choose 1, second time set to 0 (use variables in workspace)
%% Histogram of this image will be used as the reference histogram to match to
ref_image = imread('ref_image.tif');
ref_image = im2double(ref_image);
if chooseVertices == 1
[x_coord,y_coord,pixVals] = impixel(ref_image);
end
mask = roipoly(ref_image,x_coord,y_coord); %create mask from points selected with impixel
figure('Name','Mask'), imshow(mask);
[row col] = size(mask)
% Create image where all pixels are black other than the area of interest
for i=1:row
for j=1:col
if mask(i,j) == 1
ref_image_roi(i,j)= ref_image(i,j);
else
ref_image_roi(i,j) = 0;
end
end
end
figure('Name','ref_image_roi');imshow(ref_image_roi);
% %% Image whose histogram you want to match to reference histogram, repeat of above but for a different image
image1 = imread('image1.tif');
image1 = im2double(image1);
if chooseVertices == 1
[x_coord1,y_coord1,pixVals1] = impixel(image1);
end
mask1 = roipoly(image1,x_coord1,y_coord1);
figure('Name','Mask1'), imshow(mask1);
[row1 col1] = size(mask1);
for i=1:row1
for j=1:col1
if mask1(i,j) == 1
image1_roi(i,j) = image1(i,j);
else
image1_roi(i,j) = 0;
end
end
end
figure('Name','image1_roi');imshow(image1_roi);
%% MATCH HISTOGRAMS
image1_histMatch = imhistmatch(image1_roi,ref_image_roi);
figure('Name','histogram matched image');
subplot(2,2,1),imshow(image1_histMatch);
subplot(2,2,2),imhist(image1_histMatch); ylim([0, 8000])
subplot(2,2,3),imshow(ref_image_roi);
subplot(2,2,4),imhist(ref_image_roi);ylim([0, 8000])
0 Commenti
Risposte (1)
DGM
il 8 Ott 2024
Modificato: DGM
il 8 Ott 2024
There's no reason to be filling the input images with black. There's no need to use loops to do it. You already have a mask and you know how to use it, so just use it.
I don't see the point in (repeatedly) selecting only the darkest pixels in the matted images. All you're going to do is operate on the matting and some dark pixels within the ROI. It's not clear what's even intended, so I'm assuming that's just another consequence of mask avoidance.
You have two images and two masks. Just use them.
% the reference image and logical mask
refpict = imread('coins.png');
refmask = imread('coinmask.png');
% the test image and logical mask
testpict = imread('cameraman.tif');
testmask = imread('cmantifmk.png')>128;
% initialize the output
outpict = testpict; % leave the BG unchanged
%outpict = zeros(size(testpict),class(testpict)); % if you want the BG filled with black
% transform the ROI
outpict(testmask) = imhistmatch(testpict(testmask),refpict(refmask));
% show it
imshow(outpict)
0 Commenti
Vedere anche
Categorie
Scopri di più su Matrox Hardware in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!