I'm running python
program with openCV
on Raspberry-Pi
.
I got super wierd OpenCV Error
. It looks it must not be happend, but is happened.
In short, mask
s croped by cv2.inRange()
is different with same image
.
Simple structure of my code:
- process
images
intomask
and calculate kind of accuracy. - If
mask
is not gather in (1.), which meansmask
ofimage
is all black(empty)np.array
, it skip an iteration byexception: Value Error
command.(Becausemin(empty np.array)
in code araiseValue Error
.) - Iterate (1.~2.) with time interval.
Problem:
I've got same OpenCV Error
more than two weeks. It is because some parameters
in codes are assigned with wrong value. This paramters
are mostly calculated from mask
that is cropped from image
. So I saved all image
s and mask
s in the iterating procedure. And found that there were tiny mask
that must not be made at the 9th iteration
Problem is that when I tired to crop mask
with same 9 th image
manually on my labtop, it makes no mask
which means just empty(black) mask
is made. but there were very small mask
(white region) is made during iterative running in Raspberry-Pi
.
I've checked if there are difference between files in labtop and R-Pi
, but both were same.
Here is the images
:
First is input image to make Real
instance. Second is manually created mask
. Thired is Automatically created mask
in R-Pi
.
Only difference between pc and R-Pi
is that: when I run code manually in pc, I have used saved image.jpg
from SDcard of R-Pi
. and when code is run in R-Pi
iteratively, image
is directly captured from R-pi camera module V2
I know it is verry wierd, and many people would think there are typo or something wrong in codes. But I have been suffered for this problem from last two weeks. I need any thread about this kind of problem.
Is there any possible cases that different result is gathered from same code in openCV
or R-pi
?
Here is some part of code. Real
instance is created iteratively in loop. The line occuring OpenCV Error
is marked with #
. If someone request, I will upload more. It is bit long to upload full code.
class Real:
__ob_low=np.array([25,100,50]) #27,65,100]) /25,100,50
__ob_high=np.array([33,255,255]) #[45,255,255]) /33,255,255
def __opening(self, mask):
kernel = np.ones((7, 1), np.uint8)
op = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
return op
def __init__(self, img_got,imgname):
img = img_got
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask_temp = cv2.inRange(hsv, self.__ob_low, self.__ob_high)
mask = self.__opening(mask_temp)
maskdir = "/home/pi/capstone/mask/" #
img_name = imgname #
cv2.imwrite(os.path.join(maskdir,img_name),mask) #
if not (np.any(mask)):
print("mask is empty")
array1 = np.transpose(np.nonzero(mask))
array2 = np.nonzero(mask)
ymin=min(array2[1])
ymax=max(array2[1])
xmin=min(array2[0])
xmax=max(array2[0])
self.x = xmax-xmin
self.y = ymax-ymin
self.ratio = self.x/self.y
self.img = img
self.mask = mask[(xmin):(xmax),(ymin):(ymax)]