2
$\begingroup$

I'm trying to take real time input for hand gestures with web cam, then processing the images to feed them to a neural network. I wrote this processing function to make the hand features look prominent:

def image_processing(image, count):
    roi = image[42:338, 2:298] 
    cv2.imwrite('a/'+str(count)+'.png', roi)
               
    img = cv2.imread('a/'+str(count)+'.png')
   
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray,(5,5),2)

    th3 = cv2.adaptiveThreshold(blur,10,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,2)
    ret, res = cv2.threshold(th3, 225, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) 
    res = cv2.Canny(res,100,200) 
    cv2.imshow("Canny", res)
    cv2.imwrite('a/'+str(count)+'.png', res)

--Edit--

The input and the output images are as follows :

enter image description here enter image description here

It's obvious that double lines, instead of one, are detected along the edges. I want to make them single. If I apply just Canny edge detection algo, then the edges are not very prominent.

$\endgroup$
3
  • $\begingroup$ Have you considered using a max filter? $\endgroup$ – mhdadk Apr 2 at 22:05
  • $\begingroup$ Could you share the input image? $\endgroup$ – Royi Apr 2 at 23:43
  • $\begingroup$ @Royi : Shared. Plz check the edit section. $\endgroup$ – Debbie Apr 3 at 7:58
2
$\begingroup$

I ran the following code:

clear();
close('all');
mI = im2double(imread('bCfdb.png')); %<! Loading the image
vBlurStd = [0, 0.1, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2];

mII = sum(cat(3, 0.299, 0.587, 0.114) .* mI, 3); %<! Y (Luminosity like channel)

hFigure = figure('Position', [100, 100, 1200, 900]);
hTiledChartLayout = tiledlayout(3, 3);

kk = 0;
for ii = 1:3
    for jj = 1:3
        kk = kk + 1;
        if(kk == 1)
            mE = edge(mII, 'canny');
        else
            mE = edge(imgaussfilt(mII, vBlurStd(kk)), 'canny');
        end
        nexttile();
        imshow(mE);
    end
end

set(hTiledChartLayout, 'TileSpacing', 'tight', 'Padding', 'tight');

This is the result:

enter image description here

What Does the Script?

  1. We set an array of different blur radius to evaluate the algorithm at - vBlurStd.
  2. We load the image and convert it into Double in the range [0, 1] - mI = im2double(imread('bCfdb.png'));
  3. We extract the Y channel from the YCbCr color model - mII = sum(cat(3, 0.299, 0.587, 0.114) .* mI, 3);.
  4. The algorithm is basically blurring the image with Gaussian Blur and then applying Canny Edge Detector - edge(imgaussfilt(mII, vBlurStd(kk)), 'canny');.

The images are left to right, top down by their blur radius.

$\endgroup$
5
  • $\begingroup$ Hi, I'm using Python. Did you write just the algo or is it in language other than Python? $\endgroup$ – Debbie Apr 3 at 14:57
  • $\begingroup$ It's MATLAB. But you can get very similar results with Python. Just extract the Y channel, Do a small blur and then Canny Edge Detection. $\endgroup$ – Royi Apr 3 at 17:08
  • $\begingroup$ Could you plz convert it into Python? I'm absolutely unaware of Matlab syntax which is why I don't understand how the code works. $\endgroup$ – Debbie Apr 3 at 18:56
  • $\begingroup$ @Debbie, I described the algorithm. All the ingredients are already in your code. $\endgroup$ – Royi Apr 4 at 4:27
  • $\begingroup$ Thanks..I'll check. $\endgroup$ – Debbie Apr 4 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.