Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

How to transfer OpenCV threshoded image output from one function to another for further processing?

I have created python module file, containing image processing functions. One of the function which I have created, holds several preprocessing steps, ending with the thresholding and accordingly returns binary image.

def prepare (image):
    img = cv.imread(image, cv.IMREAD_GRAYSCALE)
    #Some processing codes
    #.....................
    #Step six - Binarization - Thresholding
    ret2,threshold = cv.threshold(closing2,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
    return threshold

Full module code:

def prepare (image):
    img = cv.imread(image, cv.IMREAD_GRAYSCALE)
    kernel = np.ones((1,20),np.uint8) #Kernel for Opening and Closing
    kernel2 = np.ones((5,5),np.uint8) #Kernel for secong Closing
    #Step One Image Smoothing
    gauss = cv.GaussianBlur(img,(3,3),0) #Image smoothing
    #Step Two Opening
    opening = cv.morphologyEx(gauss, cv.MORPH_OPEN, kernel)
    #Step three Closing
    closing = cv.morphologyEx(gauss, cv.MORPH_CLOSE, kernel)
    #Step four gradients
    #gradient = cv.morphologyEx(gauss, cv.MORPH_GRADIENT, kernel)
    difference = closing -opening
    #Step five second closing
    closing2 = cv.morphologyEx(difference, cv.MORPH_CLOSE, kernel2)
    #Step six - Binarization - Thresholding
    ret2,threshold = cv.threshold(closing2,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
    return threshold

def projection (image, orientation):
    if orientation =="h":
        axis=1
    else:
        axis=0
        
    image[image == 0] = 1
    image[image == 255] = 0
    horizontal_projection = np.sum(image, axis)
    horizontal_projection[horizontal_projection <=10] =0
    
    myprojection=horizontal_projection.tolist()

    height, width = image.shape #Image size
    counter=0 #Counting the width of projection
    mylist=[] #List stores the width of non-zero projections
    condition = False # When projection is 0
    variance = {} #Dictionary for storing consecutive non-zero value start-stop indexes
    segmentIndex=1
    allRoi={} #Dictionary for storing ROIs

    for idx, x in enumerate(myprojection):
        if x > 0:
            if condition == False:
                startIndex = idx
                counter +=1
                condition = True
            else:
                counter+=1
        else:
            if condition == False:
                continue;
            else:
                stopIndex = idx
                segment =[startIndex, stopIndex]
                variance[segmentIndex]=segment
                segmentIndex +=1
                mylist.append(counter)
                counter=0
                condition = False
    ave=sum(mylist)/len(mylist)

    for key in variance:
        if (variance[key][1]-variance[key][0])<ave:
            myprojection[variance[key][0]:variance[key][1]] = [0 for aa in myprojection[variance[key][0]:variance[key][1]]]
        else:
            top= variance[key][0]
            but=variance[key][1]
            roi = image[top:but, 0:width]
            allRoi[segmentIndex]=roi
            segmentIndex +=1
    return allRoi

Inside the main python program I call the preprocessing function:

import processing as ps #Importing module
#Import Image in Grayscale mode
img = ps.prepare("../img/Merani.png")
ps.cv.imshow("ts", img) #Calling imshow on function returned image 

Program works correctly and displays image as expected; However, if I print img it only shows 0-s

print(img)
[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]

and if I call another function from the module and give the result of threshold function as an argument it doesn't return any result.

horizontal_segments = ps.projection(img, "h")

Kindly advice, what I am doing wrong and why the output of the function is displayed as expected after calling it, but if I print the returned value or handover it to another function for further processing it fails .

question from:https://stackoverflow.com/questions/65904775/how-to-transfer-opencv-threshoded-image-output-from-one-function-to-another-for

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...