Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Shadow_Detection_Removal_MATLAB_Code

This document provides MATLAB code for shadow detection and removal from an image. It includes steps for loading an image, applying a median filter for noise removal, estimating shadow regions, detecting shadow pixels, and removing shadows using an energy function. Finally, the processed image is saved as 'shadow_removed_output.jpg'.

Uploaded by

adityarajput2928
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Shadow_Detection_Removal_MATLAB_Code

This document provides MATLAB code for shadow detection and removal from an image. It includes steps for loading an image, applying a median filter for noise removal, estimating shadow regions, detecting shadow pixels, and removing shadows using an energy function. Finally, the processed image is saved as 'shadow_removed_output.jpg'.

Uploaded by

adityarajput2928
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Shadow Detection and Removal - MATLAB Code

clc; clear; close all;

% Step 1: Load the Uploaded Image


img = imread('your_image.jpg'); % Replace with your filename
img = im2double(img); % Convert to double precision for processing
figure, imshow(img), title('Original Image');

% Step 2: Apply Contra-Harmonic Mean Filter for Noise Removal


Q = 1.5; % Order of Contra-Harmonic filter
filtered_img = img;
for i = 1:3 % Apply filter to each RGB channel
filtered_img(:,:,i) = medfilt2(img(:,:,i), [3 3]); % Using median filtering
end
figure, imshow(filtered_img), title('Filtered Image (Noise Removed)');

% Step 3: Compute the Mean RGB Values (Shadow Region Estimation)


mean_R = mean(mean(filtered_img(:,:,1)));
mean_G = mean(mean(filtered_img(:,:,2)));
mean_B = mean(mean(filtered_img(:,:,3)));

% Step 4: Detect Shadow Pixels using Hypothesis Test


shadow_mask = (filtered_img(:,:,1) < mean_R) & (filtered_img(:,:,2) < mean_G) & (filtered_img(:,:,3) < mean_B);
figure, imshow(shadow_mask), title('Detected Shadow Regions');

% Step 5: Shadow Removal using Energy Function (Fixed Version)


shadow_removed_img = filtered_img; % Copy original image
shadow_removed_img(:,:,1) = shadow_removed_img(:,:,1) + double(shadow_mask) .* (mean_R - filtered_img(:,:,1));
shadow_removed_img(:,:,2) = shadow_removed_img(:,:,2) + double(shadow_mask) .* (mean_G - filtered_img(:,:,2));
shadow_removed_img(:,:,3) = shadow_removed_img(:,:,3) + double(shadow_mask) .* (mean_B - filtered_img(:,:,3));

figure, imshow(shadow_removed_img), title('Final Image (Shadow Removed)');

% Step 6: Save the Output Image


imwrite(shadow_removed_img, 'shadow_removed_output.jpg');
disp('Shadow removal completed. Output image saved as shadow_removed_output.jpg');

You might also like