MATLAB: 2D CFAR algorithm.

cfarcfar detectionimage processing

I aim is to contruct a training region, gaurd region around a cell under test in the image. The mean and standard deviation of the training region are to be compared to the cell under test.
B= training band
G= guard band
P= cell under test
k=threshold.
for i = Tr-Gr:Tr-Gr
for j =Td-Gd: Td-Gd
for p = i-14 : i-14
for q = j-12 : j-12
CUT=I(p,q);
target=I(i,j);
mt=mean(target(:));
stdt=std(target(:));
test=(abs(CUT-mt)/stdt);
if (test < K)
CUT = 0;
else
CUT = 1;
end
end
end
end
end
In the output I'm expecting a binary image with detected ship, but im unable to obatin the output.

Best Answer

Sorry, it's been so long you probably already have this, but anyway for comparison purposes, here is my version. Change the image file name or use uigetfile() to select it.
Try this:
% Demo to find ships. By Image Analyst, May 5, 2020.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
% Read in image.
folder = pwd;
baseFileName = 'ships.jpeg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the original image full size.
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Color Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.


% Set up figure properties:
% Enlarge figure to full screen.
hFig = gcf;
hFig.Units = 'Normalized';
hFig.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig.Name = 'Demo by Image Analyst';
hFig.NumberTitle = 'Off'
% Let user draw the guard region.

uiwait(msgbox('Draw the guard region'));
guardRect = drawrectangle('Color', 'green');
% Let user draw the guard region.
uiwait(msgbox('Draw the P region'));
pRect = drawrectangle('Color', 'yellow');
% Save the position because when we redisplay the image, the rects will vanish otherwise.
gRect = guardRect.Position;
pRect = pRect.Position;
% Display the original image.
subplot(4, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Color Image\n"%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Draw the rectangles again.

rectangle('Position', gRect, 'EdgeColor', 'g', 'LineWidth', 2);
rectangle('Position', pRect, 'EdgeColor', 'y', 'LineWidth', 2);
grayImage = rgb2gray(rgbImage);
% Display the gray scale image.
subplot(4, 2, 2);
imshow(grayImage, []);
axis('on', 'image');
caption = sprintf('Gray Scale Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Draw the rectangles again.
rectangle('Position', gRect, 'EdgeColor', 'g', 'LineWidth', 2);
rectangle('Position', pRect, 'EdgeColor', 'y', 'LineWidth', 2);
% Make guard mask

gRect = int32(gRect);
guardMask = true(rows, columns);
colg1 = gRect(1);
colg2 = gRect(1) + gRect(3);
rowg1 = gRect(2);
rowg2 = gRect(2) + gRect(4);
guardMask(rowg1:rowg2, colg1:colg2) = false;
% Get the mean and standard deviation in the mask region.

gMean = mean(grayImage(guardMask))
gStd = std(double(grayImage(guardMask)))
% Display the mask image.

subplot(4, 2, 3);
imshow(guardMask, []);
axis('on', 'image');
caption = sprintf('Guard Mask Image, Mean = %.2f, StDev = %.2f', gMean, gStd);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Make guard mask
pRect = int32(pRect);
pMask = false(rows, columns);
colp1 = pRect(1);
colp2 = pRect(1) + pRect(3);
rowp1 = pRect(2);
rowp2 = pRect(2) + pRect(4);
pMask(rowp1:rowp2, colp1:colp2) = true;
% Get the mean and standard deviation in the mask region.
pMean = mean(grayImage(pMask))
pStd = std(double(grayImage(pMask)))
% Display the mask image.
subplot(4, 2, 4);
imshow(pMask, []);
axis('on', 'image');
caption = sprintf('P Mask Image, Mean = %.2f, StDev = %.2f', pMean, pStd);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Extract P image
P = double(grayImage(rowp1:rowp2, colp1:colp2));
% Display the P image.

subplot(4, 2, 5);
imshow(P, []);
impixelinfo;
axis('on', 'image');
caption = sprintf('P Image, Mean = %.2f, StDev = %.2f', pMean, pStd);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Compute ratio
ratio = abs(P - gMean) ./ gStd;
% Display the P image.
subplot(4, 2, 6);
imshow(ratio, []);
impixelinfo;
axis('on', 'image');
caption = sprintf('Ratio Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Threshold the image.
kThreshold = 1.01;
shipMask = ratio > kThreshold;
% Display the shipMask image.
subplot(4, 2, 7);
imshow(shipMask, []);
impixelinfo;
axis('on', 'image');
caption = sprintf('Ship Mask Image with Threshold = %.2f', kThreshold);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
fprintf('Done running %s.m\nThank you Image Analyst!', mfilename);