binary image after thresholding on strong negative x gradien in matlab

You can achieve this by following these steps:

  1. Read the input image using imread function.
  2. Convert the input image to grayscale using rgb2gray function.
  3. Calculate the gradient of the grayscale image in x direction using imgradient function with 'sobel' method.
  4. Apply a threshold on the negative gradient values using imbinarize function with 'adaptive' method.
  5. Invert the binary image using imcomplement function.

Here is the code snippet in MATLAB that implements the above steps:

main.m
% Read input image
img = imread('input_image.jpg');

% Convert to grayscale
gray_img = rgb2gray(img);

% Calculate gradient in x direction
[grad, ~] = imgradient(gray_img, 'sobel');

% Threshold negative gradient values
bin_img = imbinarize(-grad, 'adaptive');

% Invert binary image
bin_img = imcomplement(bin_img);

% Display the binary image
imshow(bin_img);
362 chars
18 lines

gistlibby LogSnag