Each image size where different. Therefore, I had to modify the code to resize the images. However, not able to display the second image as the loop continue.

1 view (last 30 days)
clc; clear all; close all;
BaseFolder = 'C:\Users\Marilyn Urrea\Desktop\Machine Perception & Cognition\MarilynDataForDIP'; % Main Folder where folders of each Rats are saved
Rat = 'Rat1_all'; % Rat1 Folder
Types = {'BigDenStimulation','Control','SmallDenStimulation','Denervated'}; % Subfolders of Expiration and Inspiration
Inspiration = '\Inspiration\InspBigDenStimulation'; % folder in which your Inspiration images exists
Expiration = '\Expiration\ExpBigDenStimulation'; % folder in which your Expiration images exists
Subtraction = '\Subtraction\SubBigDenStimulation'; % folder in which your Subtraction images exists
file_ext = ".bmp";
InspMP = strcat(BaseFolder,'\',Rat,Inspiration,'\*',file_ext);
ExpMP = strcat(BaseFolder,'\',Rat,Expiration,'\*',file_ext);
SubOut = strcat(BaseFolder,'\',Rat,Subtraction);
Insp_ds = imageDatastore(InspMP);
Exp_ds = imageDatastore(ExpMP);
firstImage = true;
minRows = Inf;
minCols = Inf;
while hasdata(Insp_ds)
[img,info] = read(Insp_ds); % we made need to use readimage if the sort (default) order mismatch
Rows = size(img,1);
Cols = size(img,2);
if (firstImage)
minRows = Rows;
minCols = Cols;
firstImage = false;
else
minRows = min([minRows,Rows]);
minCols = min([minCols,Cols]);
% if isempty(minRows)
% temp = 0
% end
end
end
while hasdata(Exp_ds)
[img,~] = read(Exp_ds);
Rows = size(img,1);
Cols = size(img,2);
minRows = min([minRows,Rows]);
minCols = min([minCols,Cols]);
end
reset(Insp_ds);
reset(Exp_ds);
firstImage = true;
while hasdata(Insp_ds)
[imgI,info] = read(Insp_ds);
inspFileName = extractFileName(info.Filename);
imgI = getMiddle(imgI,minRows,minCols);
[imgE,info] = read(Exp_ds);
expFileName = extractFileName(info.Filename);
imgE = getMiddle(imgE,minRows,minCols);
imgS = imgI - imgE;
output_image_folder = SubOut;
output_image_fileName = strcat(inspFileName, "_sub_", expFileName);
output_histeq_fileName = strcat(output_image_fileName, "_histeq");
file_ext = ".bmp";
output_image_path = strcat(output_image_folder,'\',output_image_fileName, file_ext);
output_histeq_path = strcat(output_image_folder,'\',output_histeq_fileName, file_ext);
img_gray = rgb2gray(imgS);
I = imrotate(img_gray,270);
if firstImage
figure(1), imshow(I); % creates a new window for each image
title("Original X-ray Image")
xlabel("Select first top left and then secondly the bottom right corner")
disp('Click first the top left and then secondly the bottom right corner')
[x,y] = ginput(2);
x = uint16(x);
y = uint16(y);
x_rec = [x(1) x(1) x(2) x(2)];
y_rec = [y(1) y(2) y(2) y(1)];
BW = roipoly(I,x_rec,y_rec);
firstImage = false;
J = I.*uint8(BW);
figure(2),imshow(J);
K = I(y(1): y(2), x(1): x(2));
title("Region of Interest to be Analyzed")
% Be able to cut in half and keep both sides since the goal is to
% subtract both sides and has a ratio
figure(3),imshow(K);
title("Select the vertical slice point to cut the image in half and click the side you want to keep.")
disp('Select the vertical slice point to cut the image in half')
[x_slice,~] = ginput(1);
x_slice = uint16(x_slice);
disp('Click which side you want to keep')
[x_select,~] = ginput(1);
x_select = uint16(x_select);
else
J = I.*uint8(BW);
K = I(y(1): y(2), x(1): x(2));
end
if x_select < x_slice
L = K(:,1:x_slice);%keeping the left side
else
L = K(:,x_slice:end);%keepin the right side
end
pout = L;
pout_imadjust = imadjust(pout);
pout_histeq = histeq(pout);
pout_adapthisteq = adapthisteq(pout);
figure(4)
montage({pout,pout_imadjust,pout_histeq,pout_adapthisteq},"Size",[1 4])
title("Original Image and Enhanced Images using imadjust, histeq, and adapthisteq")
figure(5)
subplot(1,4,1)
histogram(pout)
y_L = ylim();
xlim([0, 255])
subplot(1,4,2)
histogram(pout_imadjust)
xlim([0, 255])
ylim(y_L)
subplot(1,4,3)
histogram(pout_histeq)
xlim([0, 255])
ylim(y_L)
subplot(1,4,4)
histogram(pout_adapthisteq)
xlim([0, 255])
ylim(y_L)
sgtitle("Histograms of Original Image and Enhanced Images using imadjust, histeq, and adapthisteq")
end
function imgCenter = getMiddle(img,minRows,minCols)
Rows = size(img,1);
Cols = size(img,2);
firstRow = floor((Rows - minRows)/2);
firstCol = floor((Cols - minCols)/2);
lastRow = firstRow + minRows-1;
lastCol = firstCol + minCols-1;
imgCenter = img(firstRow:lastRow,firstCol:lastCol,:);
end
function [fileName, folder] = extractFileName(path)
slashLocations = strfind(path,'\');
lastSlash = max(slashLocations);
folder = path(1:lastSlash-1);
fileName = path(lastSlash + 1:end-4);
end

Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!