I this the right way to get a ROI and inside of it do the processing?

asked 2018-08-20 17:45:06 -0600

pipeecs780 gravatar image

Hello! I'm tryng to do the image processing inside of a determinate ROI, but I don't know f this its the right way to do it. I just draw a rectangle and crop the image. But I have a doubt about it, from CLAHE the image get noisy with some lines through it. I thought if I just select a ROI the image couldn't get that noisy if I select some specific area from it.

Hope you can help me

Greetings!

import cv2
import numpy as np
import math

kernel = np.ones((3,3),np.uint8)
kernel[0,0]=0
kernel[0,2]=0
kernel[2,0]=0
kernel[2,2]=0
print(kernel)

cap = cv2.VideoCapture(0)
while(cap.isOpened()):
    # read image
    ret, img = cap.read()

    # get hand data from the rectangle sub window on the screen
    cv2.rectangle(img, (200,200), (500,400), (0,255,0),0)
    crop_img = img[200:400, 200:400]

    # convert to grayscale
    gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)

##-----PROCESAMIENTO----------------------------------- 
    #Def. ROI
    nf,nc=gray.shape
#    nf3=round(nf/3)
#    gray = gray[nf3:2*nf3,:]
##    value = (5, 5)
##    gray = cv2.GaussianBlur(gray, value, 0)
    CAP1 = gray.copy()
    #EcualizaciĆ³n
    clahe = cv2.createCLAHE(clipLimit=20.0, tileGridSize=(4,4))
    gray= clahe.apply(gray)
#    gray = cv2.equalizeHist(gray)
    CAP2 = gray.copy()
    cv2.imshow('CLAHE',CAP2)
    gray = cv2.medianBlur(gray,3) #Filtro Mediana
    CAP3 = gray.copy()
    #cv2.imshow('BLUR',CAP3)
    retval,CAP4 = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    cv2.imshow('bin',CAP4)
    mask1=(CAP4>0)*gray #pixeles grises
    mask2=(CAP4==0)*int(CAP3.mean()) #Fondo Promedio
    gray=np.uint8(mask1+mask2) #conviente a formato int
    CAP5 = gray.copy()
    cv2.imshow('PROMEDIO',CAP5)
    retval,gray = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
    gray = (CAP1>0)*gray    
    CAP6 = gray.copy()
    gray = cv2.erode(gray,kernel,iterations = 2)
    gray = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel)
    CAP7 = gray.copy()
    # show thresholded image
    cv2.imshow('Thresholded', CAP7)
    cv2.imshow('Gesture', img)

    k = cv2.waitKey(10)
    if k == 27:
        break
cv2.destroyAllWindows()
cap.release()

image description

image description

edit retag flag offensive close merge delete