Untitled
unknown
plain_text
5 months ago
10 kB
3
Indexable
1. Intensity Level Slicing with Highlighting a Specific Range matlab Copy code % Intensity level slicing (highlighting a specific range) img = imread('image.jpg'); gray_img = double(0.2989 * img(:, :, 1) + 0.5870 * img(:, :, 2) + 0.1140 * img(:, :, 3)); % Parameters for intensity level slicing low_threshold = 100; % Lower bound of the intensity range high_threshold = 200; % Upper bound of the intensity range highlight_value = 255; % Value to set for pixels within the range sliced_img = zeros(size(gray_img)); for i = 1:size(gray_img, 1) for j = 1:size(gray_img, 2) if gray_img(i, j) >= low_threshold && gray_img(i, j) <= high_threshold sliced_img(i, j) = highlight_value; % Highlight the pixel else sliced_img(i, j) = gray_img(i, j); % Keep the original value end end end imshow(uint8(sliced_img)); title('Intensity Level Slicing (Highlighting Specific Range)'); 2. Intensity Level Slicing with Binary Mask matlab Copy code % Intensity level slicing with binary mask approach img = imread('image.jpg'); gray_img = double(0.2989 * img(:, :, 1) + 0.5870 * img(:, :, 2) + 0.1140 * img(:, :, 3)); % Parameters for intensity level slicing low_threshold = 100; % Lower bound of the intensity range high_threshold = 200; % Upper bound of the intensity range inside_value = 255; % Value for pixels within the range outside_value = 0; % Value for pixels outside the range binary_sliced_img = zeros(size(gray_img)); for i = 1:size(gray_img, 1) for j = 1:size(gray_img, 2) if gray_img(i, j) >= low_threshold && gray_img(i, j) <= high_threshold binary_sliced_img(i, j) = inside_value; % Set to high value else binary_sliced_img(i, j) = outside_value; % Set to low value end end end imshow(uint8(binary_sliced_img)); title('Intensity Level Slicing (Binary Mask Approach)'); 1. Reading and Displaying Images using subplot matlab Copy code % Read and display images using subplot img = imread('image.jpg'); subplot(1, 2, 1); imshow(img); title('Original Image'); % Convert to grayscale manually gray_img = 0.2989 * img(:, :, 1) + 0.5870 * img(:, :, 2) + 0.1140 * img(:, :, 3); subplot(1, 2, 2); imshow(uint8(gray_img)); title('Grayscale Image'); 2. Converting Colored Image into Grayscale Without Using In-Built Functions matlab Copy code % Convert color image to grayscale img = imread('image.jpg'); gray_img = 0.2989 * img(:, :, 1) + 0.5870 * img(:, :, 2) + 0.1140 * img(:, :, 3); imshow(uint8(gray_img)); title('Grayscale Image'); 3. Sampling and Quantization matlab Copy code % Sampling and quantization example img = imread('image.jpg'); gray_img = 0.2989 * img(:, :, 1) + 0.5870 * img(:, :, 2) + 0.1140 * img(:, :, 3); gray_img = uint8(gray_img); % Downsampling by a factor of 2 sampled_img = gray_img(1:2:end, 1:2:end); imshow(sampled_img); title('Sampled Image'); 4. Negative of an Image matlab Copy code % Image negative transformation img = imread('image.jpg'); gray_img = 0.2989 * img(:, :, 1) + 0.5870 * img(:, :, 2) + 0.1140 * img(:, :, 3); negative_img = 255 - gray_img; imshow(uint8(negative_img)); title('Negative Image'); 5. Log and Power-Law Correction matlab Copy code % Log and power-law (gamma) correction img = double(imread('image.jpg')); c = 1; % Constant for log transformation log_img = c * log(1 + img); log_img = uint8(255 * mat2gray(log_img)); imshow(log_img); title('Log Transformation'); % Power-law (gamma) transformation gamma = 2.0; % Choose gamma value power_img = c * (img .^ gamma); power_img = uint8(255 * mat2gray(power_img)); figure, imshow(power_img); title('Power-Law (Gamma) Transformation'); 6. Thresholding matlab Copy code % Thresholding without using built-in functions img = imread('image.jpg'); gray_img = 0.2989 * img(:, :, 1) + 0.5870 * img(:, :, 2) + 0.1140 * img(:, :, 3); threshold = 128; % Choose a threshold value binary_img = gray_img > threshold; imshow(binary_img); title('Thresholded Image'); 8. Contrast Stretching matlab Copy code % Contrast stretching img = imread('image.jpg'); gray_img = double(0.2989 * img(:, :, 1) + 0.5870 * img(:, :, 2) + 0.1140 * img(:, :, 3)); min_val = min(gray_img(:)); max_val = max(gray_img(:)); stretched_img = (gray_img - min_val) * (255 / (max_val - min_val)); imshow(uint8(stretched_img)); title('Contrast Stretched Image'); 9. Piecewise Linear Transformation matlab Copy code % Piecewise linear transformation img = imread('image.jpg'); gray_img = double(0.2989 * img(:, :, 1) + 0.5870 * img(:, :, 2) + 0.1140 * img(:, :, 3)); r1 = 50; s1 = 0; r2 = 200; s2 = 255; transformed_img = zeros(size(gray_img)); for i = 1:size(gray_img, 1) for j = 1:size(gray_img, 2) if gray_img(i, j) < r1 transformed_img(i, j) = (s1 / r1) * gray_img(i, j); elseif gray_img(i, j) <= r2 transformed_img(i, j) = ((s2 - s1) / (r2 - r1)) * (gray_img(i, j) - r1) + s1; else transformed_img(i, j) = ((255 - s2) / (255 - r2)) * (gray_img(i, j) - r2) + s2; end end end imshow(uint8(transformed_img)); title('Piecewise Linear Transformed Image'); 10. Histogram Equalization Without Using In-Built Functions matlab Copy code % Histogram equalization manually img = imread('image.jpg'); gray_img = double(0.2989 * img(:, :, 1) + 0.5870 * img(:, :, 2) + 0.1140 * img(:, :, 3)); [m, n] = size(gray_img); histogram = zeros(1, 256); for i = 1:m for j = 1:n histogram(gray_img(i, j) + 1) = histogram(gray_img(i, j) + 1) + 1; end end cdf = cumsum(histogram) / (m * n); equalized_img = uint8(255 * cdf(gray_img + 1)); imshow(equalized_img); title('Histogram Equalized Image'); 11. Smoothing in Spatial Domain Using Simple Average, Weighted Average, Median Filter 1. Smoothing Using Simple Average Filter matlab Copy code % Read the input image img = imread('image.jpg'); if size(img, 3) == 3 gray_img = rgb2gray(img); % Convert to grayscale if it is a color image else gray_img = img; % If already grayscale, use it directly end % Convert to double for processing gray_img = double(gray_img); % Define a simple average filter (3x3 kernel) average_filter = ones(3) / 9; % Apply the simple average filter [m, n] = size(gray_img); padded_img = padarray(gray_img, [1, 1], 'replicate'); filtered_img = zeros(m, n); for i = 2:m+1 for j = 2:n+1 % Extract the 3x3 window window = padded_img(i-1:i+1, j-1:j+1); % Apply the average filter filtered_img(i-1, j-1) = sum(sum(window .* average_filter)); end end % Display the result imshow(uint8(filtered_img)); title('Smoothed Image (Simple Average Filter)'); 2. Smoothing Using Weighted Average Filter matlab Copy code % Read the input image img = imread('image.jpg'); if size(img, 3) == 3 gray_img = rgb2gray(img); % Convert to grayscale if it is a color image else gray_img = img; % If already grayscale, use it directly end % Convert to double for processing gray_img = double(gray_img); % Define a weighted average filter (3x3 kernel) weighted_filter = [1 2 1; 2 4 2; 1 2 1] / 16; % Apply the weighted average filter [m, n] = size(gray_img); padded_img = padarray(gray_img, [1, 1], 'replicate'); filtered_img = zeros(m, n); for i = 2:m+1 for j = 2:n+1 % Extract the 3x3 window window = padded_img(i-1:i+1, j-1:j+1); % Apply the weighted average filter filtered_img(i-1, j-1) = sum(sum(window .* weighted_filter)); end end % Display the result imshow(uint8(filtered_img)); title('Smoothed Image (Weighted Average Filter)'); 3. Smoothing Using Median Filter matlab Copy code % Read the input image img = imread('image.jpg'); if size(img, 3) == 3 gray_img = rgb2gray(img); % Convert to grayscale if it is a color image else gray_img = img; % If already grayscale, use it directly end % Convert to double for processing gray_img = double(gray_img); % Apply the median filter (3x3 window) [m, n] = size(gray_img); padded_img = padarray(gray_img, [1, 1], 'replicate'); filtered_img = zeros(m, n); for i = 2:m+1 for j = 2:n+1 % Extract the 3x3 window window = padded_img(i-1:i+1, j-1:j+1); % Apply the median filter filtered_img(i-1, j-1) = median(window(:)); end end % Display the result imshow(uint8(filtered_img)); title('Smoothed Image (Median Filter)'); 12. Sharpening in Spatial Domain Using Laplacian % Read the input image img = imread('image.jpg'); if size(img, 3) == 3 gray_img = rgb2gray(img); % Convert to grayscale if it is a color image else gray_img = img; % If already grayscale, use it directly end % Convert the image to double for processing gray_img = double(gray_img); % Define the Laplacian filter laplacian_filter = [0 -1 0; -1 4 -1; 0 -1 0]; % Apply the Laplacian filter using convolution [m, n] = size(gray_img); padded_img = padarray(gray_img, [1, 1], 'replicate'); % Pad the image to handle borders sharpened_img = zeros(m, n); for i = 2:m+1 for j = 2:n+1 % Extract the 3x3 window window = padded_img(i-1:i+1, j-1:j+1); % Apply the Laplacian filter sharpened_img(i-1, j-1) = sum(sum(window .* laplacian_filter)); end end % Combine the original image with the Laplacian result to sharpen sharpened_img = gray_img - sharpened_img; % Clip values to stay within the valid range [0, 255] sharpened_img = max(min(sharpened_img, 255), 0); % Display the original and sharpened images subplot(1, 2, 1); imshow(uint8(gray_img)); title('Original Image'); subplot(1, 2, 2); imshow(uint8(sharpened_img)); title('Sharpened Image (Laplacian)'); 13. Removing Noise from an Image Removing Noise Using Median Filter matlab Copy code % Read the input image img = imread('image.jpg'); if size(img, 3) == 3 gray_img = rgb2gray(img); % Convert to grayscale if it is a color image else gray_img = img; % If already grayscale, use it directly end % Add noise to the image (for demonstration purposes) noisy_img = imnoise(gray_img, 'salt & pepper', 0.02); % Apply median filter (3x3 window) [m, n] = size(noisy_img); filtered_img = zeros(m, n); for i = 2:m-1 for j = 2:n-1 % Extract the 3x3 window window = noisy_img(i-1:i+1, j-1:j+1); % Find the median value filtered_img(i, j) = median(window(:)); end end % Display the original and filtered images subplot(1, 2, 1); imshow(noisy_img); title('Noisy Image'); subplot(1, 2, 2); imshow(uint8(filtered_img)); title('Filtered Image (Median Filter)');
Editor is loading...
Leave a Comment