예제 #1
1
파일: run_sgm.py 프로젝트: anvlason/dtm_py
def watermask(left,minsize=500):
    lds = gdal.Open(left,gdal.GA_ReadOnly)
    tdata=lds.GetRasterBand(1).ReadAsArray().astype(np.uint8)
    del(lds)
    meanshift = cv2.pyrMeanShiftFiltering(cv2.cvtColor(tdata.astype(np.uint8),cv2.COLOR_GRAY2BGR),1,30)[:,:,0]
    del(tdata)
    threshold = np.percentile(meanshift[meanshift>5],10)
    return extract_labels(np.logical_and(meanshift>5,meanshift<=threshold),minsize)>0
def approach5(img):
    print "init 5"
    imgColor = img.copy()
    imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    imgGray = cv2.equalizeHist(imgGray)

    imageEq = cv2.pyrMeanShiftFiltering(img, 40, 30)
    imageEq = cv2.cvtColor(imageEq, cv2.COLOR_BGR2HSV)
    
    minRange = cv2.cv.Scalar(1.0 , 94.0, 1.0)
    maxRange = cv2.cv.Scalar(29.0, 255.0, 255.0)
    imageEq = cv2.inRange(imageEq, minRange, maxRange)
    contours, _ = cv2.findContours(imageEq, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
    biggestContour = contours[0]
    biggestContourProps = regionProps.CalcContourProperties(contours[0], ["Area", "Centroid"])
    for contour in contours:
        props = regionProps.CalcContourProperties(contour, ["Area", "Centroid"])
        #cv2.drawContours(imgColor, contour, -1, (255, 255, 0))
        if(biggestContourProps["Area"] < props["Area"]):        
            biggestContour = contour
            biggestContourProps = props

    center = (int(biggestContourProps["Centroid"][0]), int(biggestContourProps["Centroid"][1]))
    cv2.drawContours(imgColor, [biggestContour], -1, (0, 255, 0), 3)
    cv2.circle(imgColor, center, 5, (0, 0, 255), (int(biggestContourProps["Area"]*.00005) + 1) ) 
    hull =  cv2.convexHull(biggestContour)
    cv2.drawContours(imgColor, [hull], -1, (0, 0, 255), 2)
    print "end 5"
    return imgColor
예제 #3
0
def black_background(image, kernel):

    shifted = cv2.pyrMeanShiftFiltering(image, 10, 39)
    gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255,
                           cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    D = ndimage.distance_transform_edt(thresh)
    localMax = peak_local_max(D, indices=False, min_distance=10,
                              labels=thresh)

    # perform a connected component analysis on the local peaks,
    # using 8-connectivity, then appy the Watershed algorithm
    markers = ndimage.label(localMax, structure=np.ones((3, 3)))[0]
    labels = watershed(-D, markers, mask=thresh)
    # create a mask
    mask2 = np.zeros(gray.shape, dtype="uint8")
    #  loop over the unique labels returned by the Watershed  algorithm for
    for label in np.unique(labels):
        # if the label is zero, we are examining the 'background' so simply ignore it
        if label == 0:
            continue
        # otherwise, allocate memory for the label region and draw
        # it on the mask
        mask2[labels == label] = 255
    return mask2
예제 #4
0
def videoframereaders(videodirectory):

    cap = cv2.VideoCapture(videodirectory)
    # define a kernel and subtract the background
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
    fgbg = cv2.createBackgroundSubtractorMOG2()
    timestamp = []
    count = 0
    try:
        while cap.isOpened():
            ret,frame = cap.read()
            time = cap.get(0)
            timestamp.append(time)
            print timestamp

            if frame == None:
                break;
            image = frame
            fgmask = fgbg.apply(image)
            fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)

            cv2.imshow('frame', fgmask)

            #take the image and perform pyramid mean shift filtering to aid the thresholding step
            fgmask = cv2.cvtColor(fgmask,cv2.COLOR_GRAY2BGR)
            shifted = cv2.pyrMeanShiftFiltering(fgmask, 10, 10)
            print shifted
            cv2.imshow("Input", image)
            gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY)
            thresh = cv2.threshold(gray, 0, 255,
                cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
            cv2.imshow("Thresh", thresh)
            L = measure.label(thresh)
            print "Number of components:", np.max(L)

            cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
            print("[INFO] {} unique contours found".format(len(cnts)))

            # loop over the contours
            for (i, c) in enumerate(cnts):
                # draw the contour
                ((x, y), _) = cv2.minEnclosingCircle(c)
                #cv2.putText(image, "*{}".format(i + 1), (int(x) - 10, int(y)),
                 #   cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 0)
                cv2.drawContours(image, [c], -1, (0, 255, 0), 1)

                # show the output image
                cv2.imshow("Contour", image)

                #cv2.imshow('window-name',image)
                # cv2.imwrite("/home/sami/Desktop/movies/extractFrames/frame%d.jpg" % count, image)
            count = count + 1
            sleep(5)
            if cv2.waitKey(10) & 0xFF == ord('q'):
                break


    except EOFError:
        pass
    return count,timestamp,
예제 #5
0
def __tutorial_hough_circle_detection_cv(img_path, min_dim=40, max_dim=60):
    img_color = cv2.imread(img_path)
    img_filtered = cv2.pyrMeanShiftFiltering(img_color, 10, 10)
    img_filtered = cv2.cvtColor(img_filtered, cv2.COLOR_BGRA2GRAY)
    img_filtered = img_filtered.astype(float)
    img_blurred = cv2.GaussianBlur(img_filtered, (7, 7), sigmaX=0)
    img_laplacian = cv2.Laplacian(img_blurred, ddepth=cv2.CV_64F)

    weight = 0.01 * 40
    scale = 0.01 * 20
    img_sharpened = (1.5 * img_filtered) - (0.5 * img_blurred) - (weight * cv2.multiply(img_filtered, scale * img_laplacian))
    img_sharpened = img_sharpened.astype("uint8")

    min_r = int(min_dim / 2)
    max_r = int(max_dim / 2)
    circles = cv2.HoughCircles(img_sharpened, cv2.HOUGH_GRADIENT, 1, 1, param1=50, param2=30, minRadius=min_r, maxRadius=max_r)

    if circles is not None:
        circles = np.around(circles).astype(int)
        for i in circles[0, :]:
            # draw the outer circle
            cv2.circle(img_color, (i[0], i[1]), i[2], (0, 255, 0), 2)
            # draw the center of the circle
            cv2.circle(img_color, (i[0], i[1]), 2, (0, 0, 255), 3)

    cv2.imwrite("D://_Dataset//GTSDB//Test_Regions//_img2_1.png", img_sharpened)
    cv2.imwrite("D://_Dataset//GTSDB//Test_Regions//_img2_2.png", img_color)
def approach3(img):
    print "init approach 3"
    imgColor = img.copy()
    img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    image = cv2.pyrMeanShiftFiltering(img, 40, 30)
    #cv2.imshow("pyr", image)
    minRange = cv2.cv.Scalar(1.0 , 94.0, 1.0)
    maxRange = cv2.cv.Scalar(29.0, 255.0, 255.0)
#    min = cv2.cv.Scalar(1.0 , 91.0, 89.0)
#    max = cv2.cv.Scalar(25.0, 173.0, 229.0)
    image = cv2.inRange(image, minRange, maxRange)
    #cv2.imshow("range", image)
    contours, _ = cv2.findContours(image, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
    biggestContour = contours[0]
    biggestContourProps = regionProps.CalcContourProperties(contours[0], ["Area", "Boundingbox", "Centroid", "Extend"])
    for contour in contours:
        props = tools.RegionProps.CalcContourProperties(contour, ["Area", "Boundingbox", "Centroid", "Extend"])
        if(biggestContourProps["Area"] < props["Area"]):        
            biggestContour = contour
            biggestContourProps = props

    print biggestContour
    center = (int(biggestContourProps["Centroid"][0]), int(biggestContourProps["Centroid"][1]))
    cv2.circle(imgColor, center, 5, (0, 0, 255), (int(biggestContourProps["Area"]*.00005) + 1) ) 
    cv2.drawContours(imgColor, [biggestContour], -1, (0, 255, 0))
    hull =  cv2.convexHull(biggestContour)
    cv2.drawContours(imgColor, [hull], -1, (0, 0, 255))
    print "done approach 3"
    return imgColor
예제 #7
0
def cell_shade(X):
    X_filt = cv2.pyrMeanShiftFiltering(X, 15, 30)
    X_grey = cv2.cvtColor(X, cv2.COLOR_RGB2BGRA)
    edges = cv2.Canny(X_grey, 100, 200)
    # http://stackoverflow.com/questions/3954484/cartoonizing-real-images
    edges_rgb = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)
    shaded = X_filt + 2*edges_rgb
    return shaded 
예제 #8
0
def watershed_seg(frame):
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
    fgbg = cv2.createBackgroundSubtractorMOG2()
    contors = []
    try:
        image = frame
        fgmask = fgbg.apply(image)
        fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)

        fgmask = cv2.cvtColor(fgmask,cv2.COLOR_GRAY2BGR)
        # pre-process the image before performing watershed
        # load the image and perform pyramid mean shift filtering to aid the thresholding step
        shifted = cv2.pyrMeanShiftFiltering(image, 10, 39)
        gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY)
        thresh = cv2.threshold(gray, 0, 255,
             cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
        D = ndimage.distance_transform_edt(thresh)
        localMax = peak_local_max(D, indices=False, min_distance=10,
         labels=thresh)

        # # perform a connected component analysis on the local peaks,
        # # using 8-connectivity, then appy the Watershed algorithm
        markers = ndimage.label(localMax, structure=np.ones((3, 3)))[0]
        labels = watershed(-D, markers, mask=thresh)

        print("[INFO] {} unique contour found".format(len(np.unique(labels)) - 1))

        #  loop over the unique labels returned by the Watershed
        # algorithm for finding the centriod cx and cy
        # of each contour Cx=M10/M00 and Cy=M01/M00.

        mask = np.zeros(gray.shape, dtype="uint8")
        for label in np.unique(labels):
             # if the label is zero, we are examining the 'background'
              #so simply ignore it
             if label == 0:
                 continue

            #otherwise, allocate memory for the label region and draw
             # it on the mask
             mask[labels == label] = 255
             cv2.imshow('masked', mask)
             # detect contours in the mask and grab the largest one
             cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
                 cv2.CHAIN_APPROX_SIMPLE)[-2]
             cnt = cnts[0]
             #areas = [cv2.contourArea(c) for c in cnts]
             #max_index = np.argmax(areas)
             #cnt = cnts[max_index]
             contors.append(cnt)
            # if cv2.waitKey(10) & 0xFF == ord('q'):
             #   break


    except EOFError:
        pass
    return contors
def watershedtracking(frame,d):
    xs, ys, CID = [], [], []
    try:
        image = frame

        # pre-process the image before performing watershed
        # load the image and perform pyramid mean shift filtering to aid the thresholding step
        shifted = cv2.pyrMeanShiftFiltering(image, 10, 39)
        gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY)
        thresh = cv2.threshold(gray, 0, 255,
                               cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

        D = ndimage.distance_transform_edt(thresh)
        localMax = peak_local_max(D, indices=False, min_distance=10,
                                  labels=thresh)

        # # perform a connected component analysis on the local peaks,
        # # using 8-connectivity, then appy the Watershed algorithm
        markers = ndimage.label(localMax, structure=np.ones((3, 3)))[0]
        labels = watershed(-D, markers, mask=thresh)

        #  loop over the unique labels returned by the Watershed
        # algorithm for finding the centriod cx and cy
        # of each contour Cx=M10/M00 and Cy=M01/M00.
        # generate color according to the number of identified cells
        N = len(np.unique(labels))+1

        HSV_tuples = [(B * 2.0 / N, 0.6, 0.6) for B in range(N)]
        RGB_tuples = map(lambda B: colorsys.hsv_to_rgb(*B), HSV_tuples)


        for ii, c in enumerate(labels):
            # draw the contour
            if ii > 0:
                try:
                    mask = np.zeros(gray.shape, dtype="uint8")
                    mask[labels == ii] = 255
                    # detect contours in the mask and grab the largest one
                    cv2.imwrite('/home/sami/Desktop/code/segmentation/immune_cell_seg/Immune2_%d.png' % ii, mask)
                    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
                                            cv2.CHAIN_APPROX_SIMPLE)[-2]
                    # assign a color for each cell
                    color2 = RGB_tuples[ii]
                    color3 = tuple([256 * t for t in color2])
                    cv2.drawContours(image, cnts, -1, color3, 1)
                    cv2.imshow("Contour", image)
                    cv2.waitKey(33)
                except IndexError:
                    continue
        cv2.imwrite('/home/sami/Desktop/code/segmentation/immune_cell_seg/drawContour2/Image_%d.png' % d, image)
        cv2.destroyAllWindows()
    except EOFError:
        passs

    return xs, ys
예제 #10
0
def PreFiltragem():  #Median Blur (15), Pyramid Mean Shift (35,32)
    global img_
    #Pre filtragem
    filtrado = cv.medianBlur(img_, 15)
    try:
        filtrado = cv.pyrMeanShiftFiltering(filtrado, 35, 32)
        filtrado = cv.cvtColor(filtrado, cv.COLOR_BGR2GRAY)
    except:
        print("imagem ja esta em P&B, por isso nao pode passar por um filtro")
        #adicionar algum filtro de media aqui
    return filtrado
예제 #11
0
def meanShift(imageInput):
    imageInput_unit8 = unit8Image(imageInput)
    try:
        meanShifted = cv2.pyrMeanShiftFiltering(imageInput_unit8, 20, 20)
        return meanShifted
    except TypeError: 
        print 'The input image in the function meanShift is not of data type unit8. Plese convert the image to unit8 using numpy.unit8.'
    except IOError:
        print 'The path to the file in meanShift is not correctly specified. Please check that the file is in the correct location.'
    else: 
        print 'The function meanShift is not working. You have most likely given invalid arguments.'
예제 #12
0
def onChange(pos):
    global img
    global tmp
    # tmp = np.copy(img)
    tmp = cv2.resize(img, (640,480))
    
    # get current positions of four trackbars
    ks = cv2.getTrackbarPos('kernelSize','image')
    e1 = cv2.getTrackbarPos('edgeIn1','image')
    e2 = cv2.getTrackbarPos('edgeIn2','image')
    thresh_flag = cv2.getTrackbarPos('threshInv','image')
    apertureSize = cv2.getTrackbarPos('ApertureSize','image')
    minLineLength = cv2.getTrackbarPos('LineLength','image')
    maxLineGap = cv2.getTrackbarPos('LineGap','image')
    desiredThresh = cv2.getTrackbarPos('Threshold','image')

    if apertureSize % 2 == 0:
        apertureSize += 1
    if apertureSize < 3:
        apertureSize = 3

    test = cv2.pyrMeanShiftFiltering(tmp,10, 45, 3)
    gray = cv2.cvtColor(test,cv2.COLOR_BGR2GRAY)

    if thresh_flag == 0:
        ret, thresh = cv2.threshold(gray,128,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
    if thresh_flag == 1:
        ret, thresh = cv2.threshold(gray,128,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

    edges1 = cv2.Canny(tmp,e1,e2,apertureSize = 3)
    # cv2.imshow("edges", edges1)

    res3 = cv2.bitwise_and(img, img, mask = thresh)
    kernel = np.ones((ks,ks),np.uint8)

    opening = cv2.morphologyEx(res3,cv2.MORPH_OPEN,kernel, iterations = 2)
    cv2.imshow('opened',opening)
    closing = cv2.morphologyEx(res3,cv2.MORPH_CLOSE,kernel, iterations = 2)
    cv2.imshow('closed',closing)

    if thresh_flag == 0:
        edges = cv2.Canny(opening,e1,e2,apertureSize = 3)
        gray2 = cv2.cvtColor(opening,cv2.COLOR_BGR2GRAY)
    if thresh_flag == 1:
        edges = cv2.Canny(closing,e1,e2,apertureSize = 3)
        gray2 = cv2.cvtColor(closing,cv2.COLOR_BGR2GRAY)

    ret, helper = cv2.threshold(gray2,128,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

    hlines = helper
    lines = cv2.HoughLinesP(hlines,1,np.pi/180,desiredThresh,minLineLength,maxLineGap)
    for x in range(0, len(lines)):
        for x1,y1,x2,y2 in lines[x]:
            cv2.line(tmp,(x1,y1),(x2,y2),(0,255,0),2)
예제 #13
0
def meanShift(imageInput):
    imageInput_unit8 = unit8Image(imageInput)
    try:
        meanShifted = cv2.pyrMeanShiftFiltering(imageInput_unit8, 20, 20)
        return meanShifted
    except TypeError:
        print 'The input image in the function meanShift is not of data type unit8. Plese convert the image to unit8 using numpy.unit8.'
    except IOError:
        print 'The path to the file in meanShift is not correctly specified. Please check that the file is in the correct location.'
    else:
        print 'The function meanShift is not working. You have most likely given invalid arguments.'
예제 #14
0
def shift_demo(image):
    # pyrMeanShiftFiltering(src, sp, sr, dst=None, maxLevel=None, termcrit=None)
    # @param src 8位,3通道图像
    # @param dst 与原图有相同的格式
    # @param sp 漂移物理空间半径大小
    # @param sr 漂移色彩空间半径大小
    # @param maxLevel Maximum level of the pyramid for the segmentation.
    # @param termcrit Termination criteria: when to stop meanshift iterations.
    # 关键参数是sp和sr的设置,二者设置的值越大,对图像色彩的平滑效果越明显,同时函数耗时也越多
    dst = cv.pyrMeanShiftFiltering(image, 10, 50)
    cv.imshow("shift_demo", dst)
예제 #15
0
def watershed_demo(image):
    print("image shape:" + str(image.shape))
    blurred = cv.pyrMeanShiftFiltering(image, 10, 50)
    gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)

    # morphology operation
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
    mb = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel, iterations=3)
    mb = cv.morphologyEx(binary, cv.MORPH_CLOSE, kernel, iterations=4)

    return mb
예제 #16
0
 def cut_and_enhance(self):
     self.cut_image(p=1)
     src = cv.imread(self.image_path)
     # 边缘保留滤波  去噪
     blur = cv.pyrMeanShiftFiltering(src, sp=8, sr=60)
     # 灰度图像
     gray = cv.cvtColor(blur, cv.COLOR_BGR2GRAY)
     # 二值化:设置阈值,自适应阈值的话,黄色的 4 会提取不出来
     _, binary = cv.threshold(gray, 185, 255, cv.THRESH_BINARY_INV)
     # 逻辑运算:让背景为白色,字体为黑色,便于识别
     cv.bitwise_not(binary, binary)
     cv.imwrite(self.image_path, binary)
예제 #17
0
def anime_filter(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY)

    edge = cv2.blur(gray, (3, 3))

    edge = cv2.Canny(edge, 50, 150, apertureSize=3)

    edge = cv2.cvtColor(edge, cv2.COLOR_GRAY2BGR)

    img = cv2.pyrMeanShiftFiltering(img, 5, 20)

    return cv2.subtract(img, edge)
예제 #18
0
def detect_circle_demo(image):
    dst = cv.pyrMeanShiftFiltering(image, 10, 100)  # 模糊
    gray = cv.cvtColor(dst, cv.COLOR_BGR2GRAY)  # 灰度
    circles = cv.HoughCircles(
        gray, cv.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)
    # cv.HOUGH_GRADIENT,意思是用梯度的方法来做,比较的快
    # 20:最小距离,当识别出来的的两个圆的圆心距离小于这个值,识别为一个圆,否则识别为两个圆
    circles = np.uint16(np.around(circles))
    for i in circles[0, :]:
        cv.circle(image, (i[0], i[1]), i[2], (0, 0, 255), 2)
        cv.circle(image, (i[0], i[1]), 2, (255, 0, 0), 2)
    cv.imshow('detect_circle_demo', image)
예제 #19
0
    def mean_shift_filter(self, spatial_radius=10, colour_radius=10):
        """ Apply a mean shift filter to produce a cleaner, more homogenous image.
        """

        # convert the 3 channel image to 1 channel
        if not self.image_colour:
            self._convert_image_to_colour()
            self.image_colour = True

        # apply the mean shift filter
        self.image = cv2.pyrMeanShiftFiltering(self.image, spatial_radius,
                                               colour_radius)
예제 #20
0
def preProcessImageWatershed(image, debug):
    shifted = cv2.pyrMeanShiftFiltering(image, 40, 80)
    gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255,
                           cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    if debug:
        cv2.imshow("Original", image)
        cv2.imshow("PyramidMeanShiftFilter", shifted)
        cv2.imshow("Gray", gray)
        cv2.imshow("Thresh", thresh)
        cv2.waitKey(0)
    return thresh
def saliency(img):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#    img = img[:,:,0]
#    img = cv2.GaussianBlur(img,(5,5),0)
    backproj = np.uint8(backproject(img, img, levels = 2))
    cv2.normalize(backproj,backproj,0,255,cv2.NORM_MINMAX)

    saliencies = [backproj, backproj, backproj]
    saliency = cv2.merge(saliencies)
    cv2.pyrMeanShiftFiltering(saliency, 20, 200, saliency, 1)
    saliency = cv2.cvtColor(saliency, cv2.COLOR_BGR2GRAY)
    cv2.equalizeHist(saliency, saliency)
#    plt.imshow(saliency)
#    plt.show()
    (T, saliency) = cv2.threshold(saliency, 180, 255, cv2.THRESH_BINARY)
#    T,saliency = cv2.threshold(saliency,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
#    grab_cut = refine_saliency_with_grabcut(img, saliency)  
#
#    (T, saliency) = cv2.threshold(grab_cut, 200, 255, cv2.THRESH_BINARY)  
    
    return saliency
예제 #22
0
def scale(img):
    """
    Take in image
    Reshape it to have width of 600 pixels
    Use OpenCV mean shift to recolor each pixel by shifting it towards
      the mode of a given radius of pixels
    Return recolored image
    """
    m, n = img.shape[:2]
    img = cv.resize(img, (600, int(600 * (m / n))))
    shiftImg = cv.pyrMeanShiftFiltering(img, 50, 50, 2)
    return shiftImg
예제 #23
0
def binarizeImage(image):
    # load the image and perform pyramid mean shift filtering
    # to aid the thresholding step
    shifted = cv2.pyrMeanShiftFiltering(image, 21, 51)

    # convert the mean shift image to grayscale, then apply
    # Otsu's thresholding
    gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255,
                           cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

    return thresh
예제 #24
0
def PreFiltragem(imagem):  #Median Blur (15), Pyramid Mean Shift (35,32)
    #Pre filtragem
    brilho = 0
    filtrado = cv.medianBlur(imagem, 11)
    #filtrado = cv.convertScaleAbs(filtrado, brilho,1.2,-10)
    try:
        filtrado = cv.pyrMeanShiftFiltering(filtrado, 35, 32)
        filtrado = cv.cvtColor(filtrado, cv.COLOR_BGR2GRAY)
    except:
        print("imagem ja esta em P&B, por isso nao pode passar por um filtro")
        #adicionar algum filtro de media aqui
    return filtrado
예제 #25
0
def contours_detect_demo(img):
    dst = cv.pyrMeanShiftFiltering(img, 10, 100)
    gray = cv.cvtColor(dst, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_OTSU)
    cv.imshow("binary", binary)
    contours, heriachy = cv.findContours(binary, cv.RETR_TREE,
                                         cv.CHAIN_APPROX_SIMPLE)
    for i, contour in enumerate(contours):
        print(i)
        print(contour)
        cv.drawContours(img, contours, i, (255, 0, 255), thickness=2)
    cv.imshow("result", img)
예제 #26
0
def recognize_text(image):
    # 边缘保留滤波 去噪
    blur = cv.pyrMeanShiftFiltering(image, sp=8, sr=60)
    # 灰度图像
    gray = cv.cvtColor(blur, cv.COLOR_BGR2GRAY)
    # 二值化
    ret, binary = cv.threshold(gray, 0, 255,
                               cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
    # 识别
    test_message = Image.fromarray(binary)
    text = pytesseract.image_to_string(test_message)
    return text
예제 #27
0
def segmentation(obj, array1, array2):
	""" Execute segmentation using opencv.meanshift. """

	#--- convert data type -------------
	array1_64 = array1.astype(np.float64)
	array2_64 = array2.astype(np.float64)

	# ----------------------------------
	min_val = 1000.0
	max_val = 6000.0

	array1_64[ array1_64 < min_val ] = min_val
	array1_64[ array1_64 > max_val ] = max_val
	array2_64[ array2_64 < min_val ] = min_val
	array2_64[ array2_64 > max_val ] = max_val

	array1_64 -= min_val
	array1_64 //= ( max_val - min_val +1 )/256
	array2_64 -= min_val
	array2_64 //= ( max_val - min_val +1 )/256


	#--- stack layer (numpy) --------------------------------------
	np_stack_64 = np.dstack((np.dstack((array2_64, array1_64)), array1_64))

	#--- convert to byte array (numpy) -------------------------------
	np_stack = np_stack_64.astype(np.uint8)


	#--- Meanshift for nose filtering --------------------------------
	cv2.pyrMeanShiftFiltering(np_stack, 15.0, 1.025, np_stack, 6)

	#--- Meanshift for color degradation -----------------------------
	cv2.pyrMeanShiftFiltering(np_stack, 15.0, 10.0, np_stack, 6)
	#cv2.pyrMeanShiftFiltering(np_stack, 15.0, 5.0, np_stack, 6)


	print("--, finished, segmentation()")

	return np_stack
예제 #28
0
def meanshift_segment(img,sp,sr,option):
	src = img
	i=0
	if(option == 's'):	
		while(i<=sp):
			src = cv2.cvtColor(src,cv2.cv.CV_BGR2Lab,src)
			dest = create_new(src)
			cv2.pyrMeanShiftFiltering(src,i,sr,dest)
			dest=cv2.cvtColor(dest,cv2.cv.CV_Lab2BGR,dest)
			cv2.imwrite('filtered'+str(i)+'.jpeg',dest)
			src = dest
			i=i+10
	elif(option == 'r'):
		while(i<=sr):
			src = cv2.cvtColor(src,cv2.cv.CV_BGR2Lab,src)
			dest = create_new(src)
			cv2.pyrMeanShiftFiltering(src,sp,i,dest)
			dest=cv2.cvtColor(dest,cv2.cv.CV_Lab2BGR,dest)
			cv2.imwrite('filtered'+str(i)+'.jpeg',dest)
			src = dest
			i=i+10
	elif(option == 'b'):
		i=0;j=0;k=0
		while(i<=sp and j<=sr):
			src = cv2.cvtColor(src,cv2.cv.CV_BGR2Lab,src)
			dest=create_new(src)
			cv2.pyrMeanShiftFiltering(src,i,j,dest)
			dest=cv2.cvtColor(dest,cv2.cv.CV_Lab2BGR,dest)
			cv2.imwrite('filtered'+str(k)+'.jpeg',dest)
			src=dest
			i=i+10
			j=j+10
			k=k+1
예제 #29
0
def watershed(img_path):
    ColorThings = cv2.imread(img_path)
    ColorThings, SomeBinary, contours = find_ColorThings(ColorThings, "red", 0)
    print(ColorThings.shape)
    blurred = cv2.pyrMeanShiftFiltering(ColorThings, 10, 100)
    gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
    ret, binary = cv2.threshold(gray, 127, 255,
                                cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    # cv2.imshow("~binary image:", ~binary)

    binary = ~binary.copy()

    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
    mb = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=2)

    sure_bg = cv2.dilate(mb, kernel, iterations=3)
    cv2.imshow("mor-opt:", sure_bg)

    circles = cv2.HoughCircles(sure_bg,
                               cv2.HOUGH_GRADIENT,
                               1,
                               20,
                               param1=50,
                               param2=30,
                               minRadius=0,
                               maxRadius=0)
    circles = np.uint8(np.around(circles))
    for i in circles[0, :]:
        cv2.circle(ColorThings, (i[0], i[1]), i[2], (0, 0, 255), 2)
        cv2.circle(ColorThings, (i[0], i[1]), 2, (255, 0, 0), 2)
    cv2.imshow("circle", circles)

    dist = cv2.distanceTransform(mb, cv2.DIST_L2, 3)
    dist_output = cv2.normalize(dist, 0, 1.0, cv2.NORM_MINMAX)
    cv2.imshow("distance-t", dist_output * 50)

    ret, surface = cv2.threshold(dist, 1, dist.max() * 0.9, cv2.THRESH_BINARY)
    cv2.imshow("surface-bin", surface)

    surface_fg = np.uint8(surface)
    unknown = cv2.subtract(sure_bg, surface_fg)
    ret, markers = cv2.connectedComponents(surface_fg)
    print(
        "ret",
        ret,
    )

    markers = markers + 1
    markers[unknown == 255] = 0
    markers = cv2.watershed(ColorThings, markers=markers)
    ColorThings[markers == -1] = [0, 0, 255]
    cv2.imshow("result:", ColorThings)
예제 #30
0
def preProcessImageHough(image, debug):
    shifted = cv2.pyrMeanShiftFiltering(image, 10, 30)
    gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY)
    blur = cv2.medianBlur(gray, 13)
    thresh = cv2.threshold(gray, 0, 255,
                           cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    if debug:
        cv2.imshow("Original", image)
        cv2.imshow("Blur", shifted)
        cv2.imshow("Gray", gray)
        cv2.imshow("Thresh", thresh)
        cv2.waitKey(0)
    return thresh
예제 #31
0
 def shift_demo(image):  #均值迁移
     """
     均值漂移pyrMeanShiftFiltering函数:pyrMeanShiftFiltering(src, sp, sr[, dst[, maxLevel[, termcrit]]]) -> dst
     src参数表示输入图像,8位,三通道图像。
     sp参数表示漂移物理空间半径大小。
     sr参数表示漂移色彩空间半径大小。
     dst参数表示和源图象相同大小、相同格式的输出图象。
     maxLevel参数表示金字塔的最大层数。
     termcrit参数表示漂移迭代终止条件。
     """
     dst = cv2.pyrMeanShiftFiltering(image, 10, 50)
     cv2.namedWindow("shift_demo", cv2.WINDOW_NORMAL)
     cv2.imshow("shift_demo", dst)
예제 #32
0
def method_3(image):
    blurred = cv.pyrMeanShiftFiltering(image, 10, 100)  # 先均值迁移去噪声
    gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY)
    t, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)

    contours, hierarch = cv.findContours(binary, cv.RETR_EXTERNAL,
                                         cv.CHAIN_APPROX_NONE)
    for i in range(len(contours)):
        area = cv.contourArea(contours[i])
        if area < 200:
            cv.drawContours(binary, [contours[i]], 0, 0, -1)
    # binary = cv.cvtColor(binary,cv.COLOR_GRAY2RGB)
    return binary
예제 #33
0
def detect_circles(img):
    dest = cv.pyrMeanShiftFiltering(img, 10, 100)
    gray = cv.cvtColor(dest, cv.COLOR_BGR2GRAY)
    circles = cv.HoughCircles(gray,
                              cv.HOUGH_GRADIENT,
                              1,
                              40,
                              param1=50,
                              param2=30)
    circles = np.uint16(np.around(circles))
    for i in circles[0, :]:
        cv.circle(img, (i[0], i[1]), i[2], (0, 0, 255), 2)
        cv.circle(img, (i[0], i[1]), 2, (255, 0, 0), 2)
예제 #34
0
def Filtro1(
    imagem
):  #Median Blur (15), Pyramid Mean Shift (35,32)-> para Arev e Sobrestrato
    #Pre filtragem
    cv.convertScaleAbs(imagem, imagem, 2, 0)
    imagem = cv.medianBlur(imagem, 7)
    try:
        imagem = cv.pyrMeanShiftFiltering(imagem, 25, 30)
        imagem = cv.cvtColor(imagem, cv.COLOR_BGR2GRAY)
    except:
        print("imagem ja esta em P&B, por isso nao pode passar por um filtro")
        #adicionar algum filtro de media aqui
    return imagem
예제 #35
0
def watershed_demo(image):
    print(image.shape)
    blurred = cv.pyrMeanShiftFiltering(image, 10, 100)
    # gray\binary image
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
    cv.imshow("binary-image", binary)

    #morphology operotion
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
    mb = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel, iterations=2)
    sure_bg = cv.dilate(mb, kernel, iterations=3)
    cv.imshow("mor-opt ", sure_bg)
예제 #36
0
def preprocess_image(image):

	# Perform step 1 of Mean Shift Segmentation (blurring details)
	mean_shift_img = cv.pyrMeanShiftFiltering(image, 10, 101)

	# Perform OTSU's thresholding
	gray_img = cv.cvtColor(mean_shift_img, cv.COLOR_BGR2GRAY)
	thresh_value, thresh_img = cv.threshold(gray_img, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)

	# Smoothen edges: SigmaX=0 => Sigma computed from windowSize
	smooth_img = cv.GaussianBlur(thresh_img,(5,5),0)

	return smooth_img
예제 #37
0
def meanshift(request):

    image, response = process(request)

    spatial = int(request.GET.get('meanshift-Spatial')) 

    color = int(request.GET.get('meanshift-Color')) 

    image = cv2.pyrMeanShiftFiltering(image, spatial, color)

    io.imsave(response['filename'], image)

    return JsonResponse(response)
예제 #38
0
def transform(path):
    image = cv2.imread(path)
    image = cv2.resize(image, (0, 0), fx=2, fy=2)
    val = 20
    shifted = cv2.pyrMeanShiftFiltering(image, val, val)
    cv2.imshow('shifted', shifted)
    shifted = kmeans_color_quantization(shifted, clusters=5)
    gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255,
                           cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    cv2.imshow("Thresh", thresh)

    return stackAndShow(image, shifted)
예제 #39
0
def process_and_display(path):
    img = cv2.imread(path)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    blurred = cv2.pyrMeanShiftFiltering(img, 101, 131)
    gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
    ret, threshold = cv2.threshold(gray, 0, 255,
                                   cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    _, contours, _ = cv2.findContours(threshold, cv2.RETR_LIST,
                                      cv2.CHAIN_APPROX_NONE)
    cv2.drawContours(img, contours, 0, (0, 0, 255), 6)
    cv2.namedWindow('Display', cv2.WINDOW_NORMAL)
    cv2.imshow('Display', img)
    cv2.waitKey()
예제 #40
0
def shift_demo(image):
    """
        均值漂移pyrMeanShiftFiltering函数原型:pyrMeanShiftFiltering(src, sp, sr[, dst[, maxLevel[, termcrit]]]) -> dst
        src : 表示输入图像,8位,三通道图像。
        sp : 表示漂移物理空间半径大小。
        sr : 表示漂移色彩空间半径大小。
        dst : 表示和源图象相同大小、相同格式的输出图象。
        maxLevel : 表示金字塔的最大层数。
        termcrit : 表示漂移迭代终止条件。
    """
    dst = cv2.pyrMeanShiftFiltering(image, 10, 50)
    cv2.namedWindow('shift_demo', cv2.WINDOW_NORMAL)
    cv2.imshow('shift_demo', dst)
예제 #41
0
def anime_filter(img):
    # change to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY)
    # blur
    edge = cv2.blur(gray, (3, 3))
    # Canny edge
    edge = cv2.Canny(edge, 50, 150, apertureSize=3)
    # change to RGB
    edge = cv2.cvtColor(edge, cv2.COLOR_GRAY2BGR)

    img = cv2.pyrMeanShiftFiltering(img, 5, 20)

    return cv2.subtract(img, edge)
 def clusterize_rgb(self):
     spatial_radius = 35
     color_radius = 60
     mean_shift_filtered = cv2.pyrMeanShiftFiltering(self.color_map, spatial_radius, color_radius)
     for i in range(len(mean_shift_filtered)):
         for j in range(len(mean_shift_filtered[i])):
             pixel = mean_shift_filtered[i][j]
             if (pixel[0], pixel[1], pixel[2]) not in self.rgb_clusters:
                 self.rgb_clusters[(pixel[0], pixel[1], pixel[2])] = list()
             self.rgb_clusters[(pixel[0], pixel[1], pixel[2])].append([i, j])
     for color_value, pixels in list(self.rgb_clusters.items()):
         if len(pixels) < 500:
             del self.rgb_clusters[color_value]
예제 #43
0
def watershedsegmentation(frame):
    contors = []
    try:
        image = frame


        ''' cv2.imshow('draw', fgmask)
        cv2.waitKey(0)
        cv2.destroyAllWindows()'''

        # pre-process the image before performing watershed
        # load the image and perform pyramid mean shift filtering to aid the thresholding step
        shifted = cv2.pyrMeanShiftFiltering(image, 10, 39)
        gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY)
        thresh = cv2.threshold(gray, 0, 255,
             cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
        D = ndimage.distance_transform_edt(thresh)
        localMax = peak_local_max(D, indices=False, min_distance=10,
         labels=thresh)

        # # perform a connected component analysis on the local peaks,
        # # using 8-connectivity, then appy the Watershed algorithm
        markers = ndimage.label(localMax, structure=np.ones((3, 3)))[0]
        labels = watershed(-D, markers, mask=thresh)

        #  loop over the unique labels returned by the Watershed
        # algorithm for finding the centriod cx and cy
        # of each contour Cx=M10/M00 and Cy=M01/M00.

        for label in np.unique(labels):
            # if the label is zero, we are examining the 'background' so simply ignore it
            mask = np.zeros(gray.shape, dtype="uint8")
            if label == 0:
                 continue

            #otherwise, allocate memory for the label region and draw
            # it on the mask
            mask[labels == label] = 255

            #   detect contours in the mask and grab the largest one
            cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
                 cv2.CHAIN_APPROX_SIMPLE)[-2]
            cnt = cnts[0]

            contors.append(cnt)
            # if cv2.waitKey(10) & 0xFF == ord('q'):
            # break

    except EOFError:
        pass
    return contors, mask
예제 #44
0
def filterImage(img):
    '''Apply Gaussian Blur; noise filtering'''
    img2 = cv2.GaussianBlur(img, (5,5),7)
    img2 = cv2.GaussianBlur(img2, (5,5),7)
    #img2 = cv2.GaussianBlur(img2, (5,5),11)
    #img2 = cv2.GaussianBlur(img2, (5,5),11) 
    '''Apply Bilateral filter; edge-sensitive noise filtering'''
    #img2 = cv2.bilateralFilter(img2, 21, 100, 64)
    img2 = cv2.bilateralFilter(img2, 21, 75, 32)
    '''Apply MeanShift Filter; pseudo color clustering'''
    img2 = cv2.pyrMeanShiftFiltering(img2, 20,45)
    '''Downsample Image'''
    h1, w1, d1 = img.shape
    img2= cv2.resize(img2, (200,int(round(h1*(200.0/w1)))))
    return img2
예제 #45
0
	def npr(self, image, topLeft, bottomRight):
		x1 = topLeft[0]
		x2 = bottomRight[0]

		y1 = topLeft[1]
		y2 = bottomRight[1]
		try:
			crop_image = image[y1:y2, x1:x2]
			if crop_image.size > 0:
				#crop_image = cv2.medianBlur(crop_image, 5)
				for i in range(2):
					crop_image = cv2.bilateralFilter(crop_image, 3, 10, 10)
					crop_image = cv2.pyrMeanShiftFiltering(crop_image, 7, 20)
			image[y1:y2, x1:x2] = crop_image[:,:]
			#TypeError, ValueError
		except (TypeError):
			pass
		return image
예제 #46
0
	def NPR(self, marker):
		firstPoint = self.refinePoint(marker.getFirstPoint())
		secondPoint = self.refinePoint(marker.getSecondPoint())
		x1 = firstPoint[0]
		y1 = firstPoint[1]
		x2 = secondPoint[0]
		y2 = secondPoint[1]
		try:
			crop_image = self.image[y1:y2, x1:x2]
			if crop_image.size > 0:
				#crop_image = cv2.medianBlur(crop_image, 5)
				for i in range(7):
					crop_image = cv2.bilateralFilter(crop_image, 9, 20, 20)
					crop_image = cv2.pyrMeanShiftFiltering(crop_image, 14, 40)
			self.image[y1:y2, x1:x2] = crop_image[:,:]
			#TypeError, ValueError
		except (TypeError):
			pass
예제 #47
0
    def comicCuadro(self, cuadro):
        print "A este cuadro hay que hacerlo comic"

        sigma = 0.33

        gray = cv2.cvtColor(cuadro, cv2.COLOR_BGRA2GRAY)
        edges = cv2.blur(gray, (3, 3))  # this blur gets rid of some noise

        v = np.median(edges)
        lower = int(max(0, (1.0 - sigma) * v))
        upper = int(min(255, (1.0 + sigma) * v))

        edges = cv2.Canny(edges, lower, upper)
        kernel = np.ones((3, 3), dtype=np.float) / 12.0
        edges = cv2.filter2D(edges, 0, kernel)
        edges = cv2.threshold(edges, 50, 255, 0)[1]
        edges = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
        shifted = cv2.pyrMeanShiftFiltering(cuadro, 3, 20)

        return cv2.subtract(shifted, edges)
예제 #48
0
def main():	
	src = io.VideoSource()
	src.set_bgr_source('out/bgr1')
	while (True):
		# get the current bgr frame and Mean Shift Filter it
		bgr = src.get_bgr()
		shifted = cv2.pyrMeanShiftFiltering(bgr, 12, 20)
		io.show(shifted, 'shifted')

		# perform Canny edge detection on the MSF image
		can = auto_canny(shifted)
		# overlay the detected edges
		overlay = bgr.copy()
		overlay[np.where(can)] = (255,255,255)

		# show the unadultered bgr frame and the overlayed
		io.show(bgr, 'bgr')
		io.show(overlay, 'overlay')

		if io.pause():
			break
예제 #49
0
  def get_representative_color(self, color_board_img):
    self._circle = self._detect_circle(color_board_img)
    roi = self._get_hex_roi(color_board_img)
    
    # Eliminate circlular number piece
    circle_mask = self._get_circle_mask(roi, self._CIRCLE_SCALE)
    if circle_mask is not None:
      roi = CVUtils.mask_image(roi, CVUtils.invert_mask(circle_mask))
    else:
      return np.zeros(3)

    # Amplify colors to differentiate things like wheat from desert, brick, etc.
    for amp in self._COLOR_AMPLIFICATIONS:
      thresh = self._config.get(amp[0], color_board_img)
      roi = CVUtils.replace_range(CVUtils.to_hsv(roi), roi, thresh[0], thresh[1], amp[1])

    roi = cv2.pyrMeanShiftFiltering(roi, self._MEAN_SHIFT, self._MEAN_SHIFT)
    
    # Compute mean color
    channels = cv2.split(roi)
    return [np.uint8(np.mean(c[np.nonzero(c)])) for c in channels]
	def NPR(self, marker):
		firstPoint = self.refinePoint(marker.getFirstPoint())
		secondPoint = self.refinePoint(marker.getSecondPoint())
		x1 = firstPoint[0]
		y1 = firstPoint[1]
		x2 = secondPoint[0]
		y2 = secondPoint[1]
		try:
			crop_image = self.image[y1:y2, x1:x2]
			if crop_image.size > 0:
				#crop_image = cv2.medianBlur(crop_image, 5)
				#for i in range(30):
				#crop_image = cv2.bilateralFilter(crop_image, 9, 20, 20)
				crop_image = cv2.pyrMeanShiftFiltering(crop_image, 14, 40)
				gray = cv2.cvtColor(crop_image, cv2.COLOR_BGR2GRAY)
				edges = cv2.Canny(gray, 150, 150)
				#edgesBgr = cv2.cvtColor(edges, cv2.CV_GRAY2BGR)
				crop_image = crop_image - edges
			self.image[y1:y2, x1:x2] = crop_image[:,:]
			#TypeError, ValueError
		except (TypeError):
			pass
예제 #51
0
def white_background(image, kernel):

    im = cv2.threshold(image, 173, 255, cv2.THRESH_BINARY)
    im = im[1]
    dilation = cv2.dilate(im, kernel, iterations=1)
    gradient = cv2.morphologyEx(dilation, cv2.MORPH_GRADIENT, kernel)
    closing = cv2.morphologyEx(gradient, cv2.MORPH_CLOSE, kernel)


    shifted = cv2.pyrMeanShiftFiltering(closing, 10, 20)
    gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255,
                           cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    D = ndimage.distance_transform_edt(thresh)
    localMax = peak_local_max(D, indices=False, min_distance=10,
                              labels=thresh)

    # perform a connected component analysis on the local peaks,
    # using 8-connectivity, then appy the Watershed algorithm
    markers = ndimage.label(localMax, structure=np.ones((3, 3)))[0]
    labels = watershed(-D, markers, mask=thresh)

    # create a mask
    mask2 = np.zeros(image.shape, dtype="uint8")

    #  loop over the unique labels returned by the Watershed  algorithm for
    for label in np.unique(labels):
        # if the label is zero, we are examining the 'background' so simply ignore it
        if label == 0:
            continue
        # otherwise, allocate memory for the label region and draw
        # it on the mask
        mask2[labels == label] = 255
        # close gaps
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
        mask2 = cv2.morphologyEx(mask2, cv2.MORPH_OPEN, kernel)
        mask2 = cv2.morphologyEx(mask2, cv2.MORPH_CLOSE, kernel)
    return mask2
예제 #52
0
파일: project.py 프로젝트: jholmes41/cs6475
def watercolor(image):
    
    finalImage = np.zeros_like(image)
    
    imageBlur = cv2.GaussianBlur(image,(3,3),0)
    imageBlur = cv2.cvtColor(imageBlur, cv2.COLOR_BGR2GRAY)
    sobelX = cv2.convertScaleAbs(cv2.Sobel(imageBlur, cv2.CV_16S, 1, 0, ksize = 3, scale = 1, delta = 0, borderType = cv2.BORDER_DEFAULT))
    sobelY = cv2.convertScaleAbs(cv2.Sobel(imageBlur, cv2.CV_16S, 0, 1, ksize = 3, scale = 1, delta = 0, borderType = cv2.BORDER_DEFAULT))
    tempImage = cv2.addWeighted(sobelX, 0.5, sobelY, 0.5, 0)
    tempImage = cv2.cvtColor(tempImage, cv2.COLOR_GRAY2BGR)
    
    for position,value in np.ndenumerate(tempImage):
        if value <= 128:
            tempImage[position] = 255
        else:
            tempImage[position] = 0
    
    tempColorImage = cv2.pyrMeanShiftFiltering(image, 30, 30)

    finalImage = cv2.addWeighted(tempColorImage, 0.5, tempImage, 0.5, 0)
    finalImage = cv2.addWeighted(tempColorImage, 0.3, finalImage, 0.7, 0)
            
    return finalImage
예제 #53
0
파일: obj02new.py 프로젝트: wdebeaum/cabot
def segment_image(bgr_image, mask, illuminance_scale = 1):
    global storage
    bgr_image[:,:,0] = bgr_image[:,:,0] & mask
    bgr_image[:,:,1] = bgr_image[:,:,1] & mask
    bgr_image[:,:,2] = bgr_image[:,:,2] & mask
    blob_color_rect_hsv = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2HSV)
   
    # Set the Value of the color to a constant to avoid segmenting
    # based on lighting
    blob_color_rect_hsv[:,:,2] = 100

    blob_color_rect_hsv = cv2.pyrMeanShiftFiltering(blob_color_rect_hsv,60,57,
                                                    maxLevel = 0)
    
    
    
    bgr_result= cv2.cvtColor(blob_color_rect_hsv, cv2.COLOR_HSV2BGR)
    # PyrSegmentation uses a BGR-specific algorithm, so we convert back
    blob_color_rect_hsv = cv2.cvtColor(blob_color_rect_hsv, cv2.COLOR_HSV2BGR)
    
    # Workaround for image having to be a power of 2 square, crops to 256x256
    newarray = numpy.zeros((256,256,3), dtype=numpy.uint8)
    newarray[0:min(255,blob_color_rect_hsv.shape[0]), \
             0:min(255,blob_color_rect_hsv.shape[1]), :] = \
        blob_color_rect_hsv[0:min(255,blob_color_rect_hsv.shape[0]), \
                            0:min(255,blob_color_rect_hsv.shape[1]), :]
             
    
    # This is an old algorithm so it needs a storage space
    bitmap = cv.CreateImageHeader((newarray.shape[1], newarray.shape[0]),
                                  cv.IPL_DEPTH_8U, 3)
    cv.SetData(bitmap, newarray.tostring(),
               newarray.dtype.itemsize * 3 * newarray.shape[1])

    components = cv.PyrSegmentation(bitmap, bitmap, storage,1,50,20)

    return (bgr_result, blob_color_rect_hsv, components)
예제 #54
0
def segment_image(bgr_image, illuminance_scale = 1):
    global storage
                           
    blob_color_rect_luv = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2LUV)
    blob_color_rect_luv = cv2.pyrMeanShiftFiltering(blob_color_rect_luv,30,100,
                                                    maxLevel = 0)
    
    cv2.dilate(blob_color_rect_luv,None)

    # Workaround for image having to be a power of 2 square
    newarray = numpy.zeros((256,256,3), dtype=numpy.uint8)
    newarray[0:blob_color_rect_luv.shape[0], 0:blob_color_rect_luv.shape[1], :] = \
                                             blob_color_rect_luv

    bitmap = cv.CreateImageHeader((newarray.shape[1], newarray.shape[0]),
                                  cv.IPL_DEPTH_8U, 3)
    cv.SetData(bitmap, newarray.tostring(),
               newarray.dtype.itemsize * 3 * newarray.shape[1])

    components = cv.PyrSegmentation(bitmap, bitmap, storage,1,50,20)

    bgr_result= cv2.cvtColor(blob_color_rect_luv, cv2.COLOR_LUV2BGR)

    return (bgr_result, blob_color_rect_luv, components)
예제 #55
0
def watershed_segmentation(image):
    shifted = cv2.pyrMeanShiftFiltering(image, 15, 39)
    gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255,
                           cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    D = ndimage.distance_transform_edt(thresh)
    localMax = peak_local_max(D, indices=False, min_distance=10,
                              labels=thresh)

    # # perform a connected component analysis on the local peaks,
    # # using 8-connectivity, then appy the Watershed algorithm
    markers = ndimage.label(localMax, structure=np.ones((3, 3)))[0]
    labels = watershed(-D, markers, mask=thresh)

    #  loop over the unique labels returned by the Watershed
    # algorithm for finding the centriod cx and cy


    masks2 = np.zeros(gray.shape, dtype="uint8")
    for label in np.unique(labels):
        # if the label is zero, we are examining the 'background' so simply ignore it

        if label == 0:
            continue

        # otherwise, allocate memory for the label region and draw
        # it on the mask
        # mask = np.zeros(gray.shape, dtype="uint8")
        masks2[labels == label] = 255
        # cv2.imshow('mask', mask)
        # cv2.waitKey(0)
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
        #masks2 = cv2.morphologyEx(masks2, cv2.MORPH_OPEN, kernel)
       # masks2 = cv2.morphologyEx(masks2, cv2.MORPH_CLOSE, kernel)

    return masks2
예제 #56
0
# bandwidth = estimate_bandwidth(X, quantile=0.2, n_samples=500)
# ms = MeanShift(bandwidth=bandwidth, bin_seeding=True)
# labels = ms.labels_
# cluster_centers = ms.cluster_centers_


set_k_colors(color_img, h, w, 30)
color_img.save(MEDIA_ROOT + '30_color_0.png')
print('___ set 30 colors ___')


cv2_img = cv2.imread(MEDIA_ROOT + '30_color_0.png')
spatialRadius = 35
colorRadius = 25
pyramidLevels = 3
img2 = cv2.pyrMeanShiftFiltering(cv2_img, 10, colorRadius, pyramidLevels)
cv2.imwrite(MEDIA_ROOT + '30_color.png', img2)

print('___ mean shift ___')


# a = get_c_c(color_img, h, w)
# for x in a:
#     print(x)
# print(len(a))
# print('___ get count colors ___')


# set_not_white(color_img, h, w)
color_img = Image.open(MEDIA_ROOT + '30_color.png')
white_map = threshold(color_img, h, w, t=10)
예제 #57
0
#!/usr/bin/env python
import numpy as np
import cv2
import sys

img = cv2.imread(sys.argv[1])
output_img = cv2.pyrMeanShiftFiltering(img, 25, 100, 50)
cv2.imwrite(sys.argv[2], output_img)
예제 #58
0
# cv2.TERM_CRITERIA_MAX_ITER - stop the algorithm after the specified number of iterations, max_iter.
# cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER - stop the iteration when any of the above condition is met.
# max_iter - An integer specifying maximum number of iterations.In this case it is 10
# epsilon - Required accuracy.In this case it is 1
k = 50  # Number of clusters
ret, label, centers = cv2.kmeans(img_float, k, None, criteria, 50, cv2.KMEANS_RANDOM_CENTERS)
# apply kmeans algorithm with random centers approach
center = np.uint8(centers)
# Convert the image from float to unsigned integer
res = center[label.flatten()]
# This will flatten the label
res2 = res.reshape(img.shape)
# Reshape the image
cv2.imshow("K Means", res2)  # Display image
cv2.imwrite("1.jpg", res2)  # Write image onto disk
meanshift = cv2.pyrMeanShiftFiltering(img, sp=8, sr=16, maxLevel=1, termcrit=(cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 5, 1))
# Apply meanshift algorithm on to image
cv2.imshow("Output of meanshift", meanshift)
# Display image
cv2.imwrite("2.jpg", meanshift)
# Write image onto disk
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Convert image from RGB to GRAY
ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# apply thresholding to convert the image to binary
fg = cv2.erode(thresh, None, iterations=1)
# erode the image
bgt = cv2.dilate(thresh, None, iterations=1)
# Dilate the image
ret, bg = cv2.threshold(bgt, 1, 128, 1)
# Apply thresholding
예제 #59
0
def watershedsegmentation(frame, N1, nf):
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
    # fgbg = cv2.createBackgroundSubtractorMOG2()
    contors = []

    try:
        # pre-process the image before performing watershed
        # load the image and perform  deliation, gradient, closing, and pyramid mean shift filtering to aid the thresholding step
           # apply a threshold
        im = cv2.threshold(frame, 173, 255, cv2.THRESH_BINARY)
        im = im[1]
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
        dilation = cv2.dilate(im, kernel, iterations=2)
        Gradient = cv2.morphologyEx(dilation, cv2.MORPH_GRADIENT, kernel)
        closing = cv2.morphologyEx(Gradient, cv2.MORPH_CLOSE, kernel)
        cv2.imwrite('/dirLast/Gradient_%d.png' % N1, closing)

        shifted = cv2.pyrMeanShiftFiltering(closing, 10, 20)
        gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY)
        thresh = cv2.threshold(gray, 0, 255,
                               cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

        D = ndimage.distance_transform_edt(thresh)
        localMax = peak_local_max(D, indices=False, min_distance=20,
                                  labels=thresh)

        #  perform a connected component analysis on the local peaks,
        #  using 8-connectivity, then appy the Watershed algorithm
        markers = ndimage.label(localMax, structure=np.ones((3, 3)))[0]
        labels = watershed(-D, markers, mask=thresh)

        #  loop over the unique labels returned by the Watershed
        # algorithm for finding the centriod cx and cy
        # of each contour Cx=M10/M00 and Cy=M01/M00. Generate different colors for each cell
        N = len(np.unique(labels))
        HSV_tuples = [(B * 2.0 / N, 0.6, 0.6) for B in range(N)]
        RGB_tuples = map(lambda B: colorsys.hsv_to_rgb(*B), HSV_tuples)
        mask = np.zeros(gray.shape, dtype="uint8")
        count2 = 0
        NumFiles = len(glob.glob('/dirLast/*.png'))

        for label in np.unique(labels):
            # if the label is zero, we are examining the 'background' so simply ignore it
            mask = np.zeros(gray.shape, dtype="uint8")
            if label == 0:
                continue

            # otherwise, allocate memory for the label region and draw
            # it on the mask
            color2 = RGB_tuples[count2]
            color3 = tuple([256 * t for t in color2])
            mask[labels == label] = 255
            #   detect contours in the mask and grab the largest one
            cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
                                    cv2.CHAIN_APPROX_SIMPLE)[-2]
            # get the largest contour, ignore small contour
            areas = [cv2.contourArea(c) for c in cnts]
            max_index = np.argmax(areas)
            cnt = cnts[max_index]
            contors.append(cnt)
            # save some samples for further analysis
            if NumFiles < N1:
                cv2.drawContours(frame, cnt, -1, color3, 2)
                cv2.imwrite('/dirLast/Image_%d.png' % nf, frame)

    except EOFError:
        pass
    return contors, mask
예제 #60
-1
def saliency_by_backprojection(img):
	cv2.pyrMeanShiftFiltering(img, 2, 10, img, 4)

	backproj = np.uint8(backproject(img, img, levels = 2))
	cv2.normalize(backproj,backproj,0,255,cv2.NORM_MINMAX)
	saliencies = [backproj, backproj, backproj]
	saliency = cv2.merge(saliencies)

	cv2.pyrMeanShiftFiltering(saliency, 20, 200, saliency, 2)
	saliency = cv2.cvtColor(saliency, cv2.COLOR_BGR2GRAY)
	cv2.equalizeHist(saliency, saliency)
	return 255-saliency