1

I want to do foreground segmentation using opencv. I have written code that has trackbars so that I can find optimal parameters for the segmentation. When the code is executed it works, the images are shown with contours marked and etc. But when I try to move bars on the track bar. I'm getting the following error.

OpenCV(3.4.1) Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /feedstock_root/build_artefacts/opencv_1523502125490/work/opencv-3.4.1/modules/highgui/src/window.cpp, line 356
Traceback (most recent call last):
  File "forgroundsegmentation.py", line 17, in foreground_segment
    cv2.imshow('brightness preprocess', tmp)

This is my code.

import cv2
import numpy as np

winName = "ForeGround Segment"

def foreground_segment(src, a=None,b=None, useEqualize=1, blurSize=21, th1=None, brightness=None):
    winName = "ForeGround Segment"
    tmp = brightness

    if (blurSize >= 3):
        blurSize += (1 - blurSize % 2)
        tmp = cv2.GaussianBlur(tmp, (blurSize, blurSize), 0)
    if (useEqualize):
        tmp = cv2.equalizeHist(tmp)

    cv2.imshow('brightness preprocess', tmp)
    #cv2.imwrite('../BrightnessPreprocess.png', tmp)

    ret, tmp = cv2.threshold(tmp, th1, 255, cv2.THRESH_BINARY_INV)
    cv2.imshow(winName, tmp)

    im2, contours, hierarchy = cv2.findContours(tmp, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    dst = src

    #print(contours)    

    maxDim = 0
    largest = -1
    for i in range(len(contours)):
        cv2.drawContours(dst, contours, largest, (0, 0, 255), 1)
        dim = len(contours[i])
        if (dim > maxDim):
            maxDim = dim
            largest = i

    img_mask = np.zeros(src.shape, np.uint8)

    if (largest >= 0):
        theImg = contours[largest]
        cv2.polylines(dst, theImg, True, (0, 255,0), 2)
        cv2.drawContours(img_mask, contours, largest, 255, -1)

    cv2.imshow("Result Mask", img_mask)
    cv2.imshow("Result Contour", dst)
    #cv2.imwrite("../img_mask.png", img_mask)
    #cv2.imwrite("../result.png", dst)

if __name__ == '__main__':
    src = cv2.imread('frontal.png')
    print(src.shape)
    src = cv2.resize(src, (int(src.shape[0]/3), int(src.shape[1]/3)), interpolation = cv2.INTER_AREA)
    #src = cv2.resize(src, (500, 500), interpolation = cv2.INTER_AREA)
    cv2.imshow(winName, src)
    dst = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)
    hsv_planes = cv2.split(dst)
    brightness = hsv_planes[2]
    useEqualize = 1
    blurSize = 21
    th1 = int(33.0 * 255 / 100)
    cv2.createTrackbar("Equalize", winName, useEqualize, 1, foreground_segment)
    cv2.createTrackbar("Blur Sigma", winName, blurSize, 100, foreground_segment)
    cv2.createTrackbar("Threshold", winName, th1, 255, foreground_segment)

    foreground_segment(src ,0, 0, useEqualize, blurSize, th1, brightness=brightness)
    cv2.waitKey(0)
    cv2.destroyAllWindows   

I think I'm doing everything correctly. Can someone point me out where I'm going wrong.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114

1 Answers1

0

First off, building a segmentation tool of this type is a great idea!!

The code you have provided only creates the trackbars using cv2.createTrackbar(). In order to use them you need to implement cv2.getTrackbarPos() to get different values as you slide the trackbar.

I have made modified the same code:

import cv2
import numpy as np

winName = "ForeGround Segment"

def foreground_segment(im, brightness, useEqualize=1, blurSize=21, th1=None):
    winName = "ForeGround Segment"
    tmp = brightness

    if (blurSize >= 3):
        blurSize += (1 - blurSize % 2)
        tmp = cv2.GaussianBlur(tmp, (blurSize, blurSize), 0)
    if (useEqualize):
        tmp = cv2.equalizeHist(tmp)

    cv2.imshow('brightness preprocess', tmp)
    #cv2.imwrite('../BrightnessPreprocess.png', tmp)

    ret, tmp = cv2.threshold(tmp, th1, 255, cv2.THRESH_BINARY_INV)
    cv2.imshow('threshold', tmp)

    im2, contours, hierarchy = cv2.findContours(tmp, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    dst = im.copy()

    #print(contours)    

    maxDim = 0
    largest = -1
    for i in range(len(contours)):
        cv2.drawContours(dst, contours, largest, (0, 0, 255), 1)
        dim = len(contours[i])
        if (dim > maxDim):
            maxDim = dim
            largest = i

    img_mask = np.zeros(src.shape, np.uint8)

    if (largest >= 0):
        theImg = contours[largest]
        cv2.polylines(dst, theImg, True, (0, 255,0), 2)
        cv2.drawContours(img_mask, contours, largest, 255, -1)

    cv2.imshow("Result Mask", img_mask)
    cv2.imshow("Result Contour", dst)
    #cv2.imwrite("../img_mask.png", img_mask)
    #cv2.imwrite("../result.png", dst)

if __name__ == '__main__':
    src = cv2.imread('frontal.png')
    src = cv2.resize(src, (int(src.shape[0]/3), int(src.shape[1]/3)), interpolation = cv2.INTER_AREA)
    cv2.imshow(winName, src)
    dst = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)
    hsv_planes = cv2.split(dst)
    brightness = hsv_planes[2]
    useEqualize = 1
    blurSize = 21
    th1 = int(33.0 * 255 / 100)
    cv2.createTrackbar("Equalize", winName, useEqualize, 1, foreground_segment)
    cv2.createTrackbar("Blur Sigma", winName, blurSize, 100, foreground_segment)
    cv2.createTrackbar("Threshold", winName, th1, 255, foreground_segment)

    while(1):

        #--- Using cv2.getTrackbarPos() to get values from the slider ---
        useEqualize = cv2.getTrackbarPos('Equalize', winName)
        blurSize = cv2.getTrackbarPos('Blur Sigma', winName)
        th1 = cv2.getTrackbarPos('Threshold', winName)

        foreground_segment(src, brightness, useEqualize, blurSize, th1)

        #--- Press Q to quit ---            
        k = cv2.waitKey(1) & 0xFF
        if k == 27:
            break

    cv2.destroyAllWindows() 
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • Thank you very much. :) I am a beginner OpenCV user. –  Jul 01 '18 at 07:46
  • `TypeError: foreground_segment() missing 1 required positional argument: 'brightness`. This is the error I'm getting when I try moving the silder. But `brightness` is passed into the function as an argument. Why do you think this is happening. –  Jul 01 '18 at 07:56
  • @MathJunkiey Are you able to get the output? – Jeru Luke Jul 01 '18 at 08:15
  • I'm able to get the output. –  Jul 01 '18 at 08:27
  • You could try searching for algorithms built using CNN specifically for these kind of tasks. – Jeru Luke Jul 01 '18 at 09:01