Ask Your Question
0

Returning Threshold Values

asked 2018-03-17 02:00:57 -0600

Stephane gravatar image

I am trying to return the average threshold value for a binary ROI, however, I keep on getting a weird output.

My code is :

import cv2
import numpy as np

img = cv2.imread("/Users/2020shatgiskessell/Desktop/roomimage.jpg")
roomimg = cv2.resize(img, (0,0), fx=0.5, fy=0.5)
gray = cv2.cvtColor(roomimg, cv2.COLOR_BGR2GRAY)

#edge detection 
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
edge = cv2.Canny(thresh, 100, 200)

#create nodes by iterating through 10x10 blocks and checking the neighbors

height,width,channels = roomimg.shape

for i in range (0,width,10):
    for j in range (0,height,10):
        #roi is null for some reason
        roi_gray = cv2.rectangle(edge, (i,j), (i+10, j+10), (128, 128, 128), 1)
        cv2.imshow('ROI', roi_gray)
        retval, threshold = cv2.threshold(roi_gray, 10, 255, cv2.THRESH_OTSU)
        threshed_roi_gray = threshold
        print (threshed_roi_gray)
        #see what threshold value is, assign roi a 1 or 0, and add it to list to be traversed



cv2.imshow('Image Edges', edge)
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()

And the output values I get are:

[255   0   0 ...,   0   0   0]
[255   0   0 ...,   0   0   0]]
[[255 255 255 ...,   0   0   0]
[255   0   0 ...,   0   0   0]
[255   0   0 ...,   0   0   0]
....

I am confused what these values are. I thought that [255 0 0] was the color code for red, while my image is only black and white. I am also confused why 2 data values are returned for each ROI threshold, instead of just 1. I would appreciate your help, thanks.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-03-17 02:32:45 -0600

berak gravatar image

updated 2018-03-17 04:26:57 -0600

you print out the thresholded image, not the threshold value, which is in retval ;)

btw [255 0 0] would be blue, not red (BGR order in numpy/opencv)

and as always: read docs !

edit flag offensive delete link more

Comments

Thank you so much! When I print the retval value, I get 0.0 for everything. Is that just because the ROI is null for some reason?

Stephane gravatar imageStephane ( 2018-03-17 05:13:49 -0600 )edit

sorry, idk. you'll have to analyze that, locally. (can't do that from here)

(but yes, if your roi is all zeros, the threshold will be zero, too.)

berak gravatar imageberak ( 2018-03-17 05:18:27 -0600 )edit

I fixed the issue, and when I print the retval I get values like 187.0, 119.0, etc. What exactly do these values mean? Like what would a white value be and what would a black one be? Once again, I really appreciate your help. Thanks.

Stephane gravatar imageStephane ( 2018-03-17 05:57:18 -0600 )edit

... read docs....

it depends, on the flags you set there, and what is bg/fg / inverted

berak gravatar imageberak ( 2018-03-17 06:08:42 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-03-17 02:00:57 -0600

Seen: 511 times

Last updated: Mar 17 '18