Skip to content

Instantly share code, notes, and snippets.

@thorskjold

thorskjold/Exercise10.m

Last active Mar 11, 2021
Embed
What would you like to do?
clear all; close all; clc;
hand1 = imread('Hand1.jpg');
hand2 = imread('Hand2.jpg');
fprintf('Opening new window\n',figure('Name','Hand 1 and Hand 2','NumberTitle','off'));
imshowpair(hand1,hand2,'montage');
fprintf('\n')
fprintf('Exercise 1:\n')
%cpselect(hand2, hand1); %save('fixedPoints.mat','fixedPoints'); %save('movingPoints.mat','movingPoints'); load('fixedPoints.mat','fixedPoints'); load('movingPoints.mat','movingPoints');
fprintf('\n')
fprintf('Exercise 2:\n')
fprintf('Opening new window\n',figure('Name','Exercise 2','NumberTitle','off')); plot(fixedPoints(:,1), fixedPoints(:,2), 'b*-', ...
movingPoints(:,1), movingPoints(:,2), 'r*-');
legend('Hand 1 - The fixed image', 'Hand 2 - The moving image');
axis ij;
fprintf('\n')
fprintf('Exercise 3:\n')
ex = fixedPoints(:,1) - movingPoints(:,1); errorX = ex' * ex;
ey = fixedPoints(:,2) - movingPoints(:,2);
errorY = ey' * ey;
fprintf('F is %f\n',errorX + errorY) fprintf('\n')
fprintf('Exercise 4:\n')
fixed_COM = mean(fixedPoints); moving_COM = mean(movingPoints);
fprintf('\n')
fprintf('Exercise 5:\n')
fixed_trans = [fixedPoints(:,1) - fixed_COM(1) ...
fixedPoints(:,2) - fixed_COM(2)];
moving_trans = [movingPoints(:,1) - moving_COM(1) ...
movingPoints(:,2) - moving_COM(2)];
fprintf('Opening new window\n',figure('Name','Exercise 5','NumberTitle','off')); plot(fixed_trans(:,1), fixed_trans(:,2), 'b*-', ...
moving_trans(:,1), moving_trans(:,2), 'r*-');
legend('Hand 1 - The fixed image', 'Hand 2 - The moving image');
axis ij
fprintf('\n')
fprintf('Exercise 6:\n')
mytform = cp2tform(movingPoints, fixedPoints,'linear conformal');
fprintf('\n')
fprintf('Exercise 7:\n')
forward = tformfwd(mytform,movingPoints);
fprintf('Opening new window\n',figure('Name','Exercise 7','NumberTitle','off')); plot(fixedPoints(:,1), fixedPoints(:,2), 'b*-', ...
forward(:,1), forward(:,2), 'r*-');
legend('Hand 1 - The fixed image', 'Hand 2 - The moving image');
fprintf('\n')
fprintf('Exercise 8:\n')
ex = fixedPoints(:,1) - forward(:,1); errorX = ex' * ex;
ey = fixedPoints(:,2) - forward(:,2); errorY = ey' * ey;
fprintf('F is %f\n',errorX + errorY)
fprintf('\n')
fprintf('Exercise 9:\n')
hand2t = imtransform(hand2, mytform);
fprintf('Opening new window\n',figure('Name','Exercise 9','NumberTitle','off')); imshowpair(hand1,hand2t,'montage');
fprintf('\n')
fprintf('Exercise 11:\n')
BT1 = imread('BT1.png');
BT2 = imread('BT2.png');
%cpselect(BT2, BT1);
%save('BT1points.mat','BT1points');
%save('BT2points.mat','BT2points');
load('BT1points.mat','BT1points');
load('BT2points.mat','BT2points');
fprintf('Opening new window\n',figure('Name','Exercise 11','NumberTitle','off')); plot(BT1points(:,1), BT1points(:,2), 'b*-', ...
BT2points(:,1), BT2points(:,2), 'r*-');
legend('BT1 - The fixed image', 'BT2 - The moving image');
axis ij
fprintf('\n')
fprintf('Exercise 12:\n')
mytform = cp2tform(BT2points, BT1points, 'linear conformal');
TI = imtransform(BT2, mytform, 'XData',[1 800], 'YData',[1 600]); fprintf('Opening new window\n',figure('Name','Exercise 12','NumberTitle','off')); imshowpair(BT1,TI,'montage');
fprintf('\n')
fprintf('Exercise 13:\n')
%BT1_annot = roipoly(BT1);
%BT2_annot = roipoly(TI);
%imwrite(BT1_annot, 'BT1_annot.png');
%imwrite(BT2_annot, 'BT2_annot.png')
BT1_annot = imread('BT1_annot.png');
BT2_annot = imread('BT2_annot.png');
fprintf('Opening new window\n',figure('Name','Exercise 13','NumberTitle','off')); imshowpair(BT1_annot,BT2_annot,'montage');
fprintf('\n')
fprintf('Exercise 14:\n')
RGB1 = label2rgb(BT1_annot + BT2_annot);
RGB2 = label2rgb(BT1_annot + BT2_annot * 2);
fprintf('Opening new window\n',figure('Name','Exercise 14','NumberTitle','off')); imshowpair(RGB1,RGB2,'montage');
fprintf('\n')
fprintf('Exercise 15:\n')
Area1 = regionprops(BT1_annot, 'Area');
Area2 = regionprops(BT2_annot, 'Area');
fprintf('The removed area of the tumor is %f\n',17853 - 5512); fprintf('\n')
%saveFigs;
clear all; close all; clc;
%% Exercise 1
f = [zeros(5,3),ones(5,2)]; h = ones(3,3);
g = imfilter(f,h)
%% Exercise 2
fsize = 5;
h = ones(fsize)/fsize^2;
%% Exercise 3
im1 = imread('02511/Gaussian.png');
meanim1 = imfilter(im1,h);
figure; imshowpair(im1,meanim1,'montage'); title('Original and mean filtered image');
%% Exercise 4
meanim2 = imfilter(im1,h,'replicate');
figure; imshowpair(meanim1,meanim2,'montage'); title('Mean filtered image without and with border replication');
%% Exercise 5
figure; imshowpair(im1,meanim2,'montage'); title('Original image and mean filtered image with border replication');
%% Exercise 6
fsize = 15;
h = ones(fsize)/fsize^2;
meanim1 = imfilter(im1,h);
figure; imshowpair(im1,meanim1,'montage'); title('Original and mean filtered image with kernel size [15x15]');
%% Exercise 8
fsize = 5;
h = ones(fsize)/fsize^2;
meanim1 = imfilter(im1,h);
medim1 = medfilt2(im1,[5 5]);
figure; imshowpair(meanim1,medim1,'montage'); title('Mean and median filtered image with kernel size [5x5]');
%% Exercise 9
fsize = 15;
h = ones(fsize)/fsize^2;
meanim1 = imfilter(im1,h);
medim1 = medfilt2(im1,[15 15]);
figure; imshowpair(meanim1,medim1,'montage'); title('Mean and median filtered image with kernel size [15x15]');
%% Exercise 11
im2 = imread('02511/SaltPepper.png');
meanim2 = imfilter(im2,h);
medim2 = medfilt2(im2,[15 15]);
figure; imshowpair(meanim2,medim2,'montage'); title('Mean and median filtered SaltPepper image with kernel size [15x15]');
%% Exercise 12
h = ones(3,3)
fsize = 5;
h = ones(fsize)/fsize^2 h = ones(3,3)
fsize = 15;
h = ones(fsize)/fsize^2;
h = fspecial('average')
h = fspecial('average',5) h = fspecial('average',15)
%% Exercise 13
CT = imread('02511/ElbowCTSlice.png');
h = fspecial('sobel')
CTsobel = imfilter(CT,h);
figure; imshowpair(CT,CTsobel,'montage'); title('CT and sobel filtered image'); h = fspecial('prewitt')
CTprewitt = imfilter(CT,h);
figure; imshowpair(CT,CTprewitt,'montage'); title('CT and prewitt filtered image');
%% Exercise 14
h = fspecial('sobel')
CT2 = imfilter(CT,-h);
CT3 = imfilter(CT,h');
figure; imshowpair(CT2,CT3,'montage'); title('CT image with kernel rotated minus and plus 90 degrees');
%% Magnitude
x = double(CT2);
y = double(CT3);
M = sqrt((x.^2)+(y.^2));
figure; imshow(M,[]); title('Max magnitude of sobel filtered image');
%% Exercise 15
fsize = 5;
h = ones(fsize)/fsize^2;
meanCT = imfilter(CT,h);
fsize = 13;
h = ones(fsize)/fsize^2;
meanCT2 = imfilter(CT,h);
h = fspecial('sobel')
meanCTsob = imfilter(meanCT,h);
meanCTsob2 = imfilter(meanCT2,h);
figure; imshowpair(meanCTsob,meanCTsob2,'montage'); title('CT image mean filtered with sobel');
fsize = 5;
h = ones(fsize)/fsize^2;
meanCT = imfilter(CT,h);
fsize = 13;
h = ones(fsize)/fsize^2;
meanCT2 = imfilter(CT,h);
h = fspecial('prewitt')
meanCTprewitt = imfilter(meanCT,h);
meanCTprewitt2 = imfilter(meanCT2,h);
figure; imshowpair(meanCTprewitt,meanCTprewitt2,'montage'); title('CT image mean filtered with prewitt');
%% Exercise 16
medCT = medfilt2(CT,[5 5]);
medCT2 = medfilt2(CT,[13 13]);
h = fspecial('sobel')
medCTsob = imfilter(medCT,h);
medCTsob2 = imfilter(medCT2,h);
figure; imshowpair(medCTsob,medCTsob2,'montage'); title('CT image median filtered with sobel');
medCT = medfilt2(CT,[5 5]);
medCT2 = medfilt2(CT,[13 13]);
h = fspecial('prewitt')
medCTprewitt = imfilter(medCT,h);
medCTprewitt2 = imfilter(medCT2,h);
figure; imshowpair(medCTprewitt,medCTprewitt2,'montage'); title('CT image median filtered with prewitt');
%% Exercise 17
CTedgesobel = edge(CT,'sobel');
CTedgeprewitt = edge(CT,'prewitt');
figure; imshowpair(CTedgesobel, CTedgeprewitt,'montage'); title('CT image edge filtered with sobel and prewitt');
%% Exercise 18
h = 17;
sigma = 3;
G = fspecial('gaussian',h,sigma); surf(G); title('Gaussian filter kernel');
%% Exercise 19
h = 17;
sigma = 1;
G = fspecial('gaussian',h,sigma);
gausCT = imfilter(CT,G);
sigma = 3;
G = fspecial('gaussian',h,sigma);
gausCT2 = imfilter(CT,G);
figure; imshowpair(gausCT,gausCT2,'montage'); title('CT image gaussian filtered');
h = 51;
sigma = 11;
G = fspecial('gaussian',h,sigma);
gausCT3 = imfilter(CT,G);
figure; imshow(gausCT3); title('CT image gaussian filtered with larger kernel size');
%% Exercise 20
im = imread('02511/image.jpg');
imgray = rgb2gray(im);
figure; imshowpair(im,imgray,'montage'); title('RGB image and Grayscale image');
%% Exercise 21
h = fspecial('sobel');
imgraysobel = imfilter(imgray,h);
h = fspecial('prewitt');
imgrayprewitt = imfilter(imgray,h);
figure; imshowpair(imgraysobel,imgrayprewitt,'montage'); title('Grayscale image mean filtered with sobel and with prewitt');
imgrayedgesobel = edge(imgray,'sobel'); h = 10;
sigma = 5;
G = fspecial('gaussian',h,sigma); imgraygaus = imfilter(imgray,G);
figure; imshowpair(imgrayedgesobel,imgraygaus,'montage'); title('Grayscale image edge filtered with sobel and with gaussian filter');
clear all; close all; clc;
%%
ct = dicomread('Training.dcm'); figure;
imshow(ct, [-100,200]);
I = imread('TrainingScaled.png');
%% Exercise 1
%SpleenRoi = roipoly(I); %imwrite(SpleenRoi, 'SpleenROI.png');
SpleenROI = imread('SpleenROI.png'); SpleenVals = double(ct(SpleenROI));
%% Exercise 2
figure;
hist(SpleenVals); title('New SpleenVals'); mean(SpleenVals)
std(SpleenVals)
%% Exercise 3
SpleenValsOld = imread('SpleenVals.png'); figure;
imshow(SpleenValsOld);
%%
xrange = -1200:0.1:1200;
pdfFitSpleen = normpdf(xrange, mean(SpleenVals), std(SpleenVals)); figure;
S = length(SpleenVals);
hold on;
hist(SpleenVals,xrange);
plot(xrange, pdfFitSpleen * S,'r');
hold off;
xlim([2, 92]);
%% Exercise 4 T1 = 20;
T2 = 80;
%%
binI = (ct > T1) & (ct < T2); figure;
imshow(binI);
%% Exercise 6
se = strel('disk',1);
closeBinI = imclose(binI,se); figure;
imshow(closeBinI);
%% Exercise 7
se = strel('disk',10);
openBinI = imopen(closeBinI,se); figure;
imshow(openBinI);
IMorph = openBinI;
%%
L8 = bwlabel(IMorph,8); RGB8 = label2rgb(L8); figure;
imagesc(RGB8);
%%
stats8 = regionprops(L8, 'All');
idx = find([stats8.Area] > 1000 & [stats8.Perimeter] < 400); BW = ismember(L8,idx);
figure;
imagesc(BW);
axis image;
title('Spleen candidate');
%% Exercise 9
idx = find([stats8.Area] > 3500 & [stats8.Perimeter] < 350 & [stats8.Eccentricity] < 0.9099); BW = ismember(L8,idx);
figure;
imagesc(BW);
axis image;
title('Spleen candidate');
%%
info = dicominfo('Validation3.dcm'); info.PixelSpacing;
%% Exercise 10
info = dicominfo('Training.dcm');
stats8(idx).Area * info.PixelSpacing(1) * info.PixelSpacing(2)
%% Exercise 11 Algorithm(dicomread('Validation1.dcm')); Algorithm(dicomread('Validation2.dcm')); Algorithm(dicomread('Validation3.dcm'));
imshowpair(Algorithm(dicomread('Validation1.dcm')),dicomread('Validation1.dcm'),'diff'); imshowpair(Algorithm(dicomread('Validation2.dcm')),dicomread('Validation2.dcm'),'diff'); imshowpair(Algorithm(dicomread('Validation3.dcm')),dicomread('Validation3.dcm'),'diff');
%% Exercise 13 Algorithm2(dicomread('Validation1.dcm')); Algorithm2(dicomread('Validation2.dcm')); Algorithm2(dicomread('Validation3.dcm'));
imshowpair(Algorithm(dicomread('Validation1.dcm')),dicomread('Validation1.dcm'),'diff'); imshowpair(Algorithm(dicomread('Validation2.dcm')),dicomread('Validation2.dcm'),'diff'); imshowpair(Algorithm(dicomread('Validation3.dcm')),dicomread('Validation3.dcm'),'diff');
%% saveFigs;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment