Ask Your Question
1

Backprojection not working when tried with BGR instead of HSV

asked 2018-01-19 07:22:26 -0600

Santhosh1 gravatar image

updated 2018-01-19 07:23:29 -0600

I tried using BGR instead of using HSV for both ROI and target on code as on Histogram - 4 : Histogram Backprojection. Even after making changes for the 3 channels for BGR. I'm not finding any difference

Here's the changed code

# calculating object histogram
roihist = cv2.calcHist([roi],[0, 1, 2], None, [256,256,256], [0, 256, 0, 256, 0, 256] )

# normalize histogram and apply backprojection
cv2.normalize(roihist,roihist,0,255,cv2.NORM_MINMAX)
dst = cv2.calcBackProject([target],[0,1,2],roihist,[0,256,0,256,0,256],1)

# Now convolute with circular disc
disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
cv2.filter2D(dst,-1,disc,dst)

# threshold and binary AND
ret,thresh = cv2.threshold(dst,50,255,0)
thresh = cv2.merge((thresh,thresh,thresh))
res = cv2.bitwise_and(target,thresh)

Can anyone explain why is it not working with BGR and only with HSV?

edit retag flag offensive close merge delete

Comments

Are you sure that 's OK in HSV? @berak i think there is a bug in opencv-python when you give three channels

LBerger gravatar imageLBerger ( 2018-01-25 03:29:56 -0600 )edit
1

My program is

import numpy as np
import cv2 as cv
img = cv.imread('c:/lib/opencv/samples/data/fruits.jpg',cv.IMREAD_COLOR)
orange = img[300:380,150:220]
cv.imshow('fruits',img)
cv.imshow('orange',orange)
# calculating object histogram
c=[0,1,2]
r=[32,32,32]
rc=[  0, 256,0,256,0,256]
roihist = cv.calcHist([orange],c, None, r, rc )
# normalize histogram and apply backprojection
cv.normalize(roihist,roihist,0,255,cv.NORM_MINMAX)
dst = cv.calcBackProject([img],c,roihist,rc,1)
ret,thresh = cv.threshold(dst,10,255,0)
thresh = cv.merge((thresh,thresh,thresh))
cv.imshow('dst',dst)
cv.imshow('res',thresh)
cv.waitKey(0)
cv.destroyAllWindows()

thresh image is black. In c++ I have got a good result.

LBerger gravatar imageLBerger ( 2018-01-25 03:54:16 -0600 )edit
1

when I set

 c=[0,2]
 r=[32,32]
 rc=[  0,256,0,256]

result is good. I don't understand what is wrong in 3D

LBerger gravatar imageLBerger ( 2018-01-25 03:55:35 -0600 )edit

ok, i can reproduce it. (but no idea, so far)

berak gravatar imageberak ( 2018-01-25 04:04:24 -0600 )edit
1

I think problem is https://github.com/opencv/opencv/blob... . I don't call this function in C++ I call https://docs.opencv.org/master/d6/dc7...

LBerger gravatar imageLBerger ( 2018-01-25 04:18:54 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-01-25 05:26:45 -0600

LBerger gravatar image

issue and PR :

edit flag offensive delete link more

Comments

1

@berak PR is merged and now it works with :

c=[2,1,0]
r=[32 ,32,32]
rc=[  0, 256,0, 256,0,256]

but not with c=[0,1,2] r=[32 ,32,32] rc=[ 0, 256,0, 256,0,256]

LBerger gravatar imageLBerger ( 2018-01-28 11:48:58 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-01-19 07:22:26 -0600

Seen: 432 times

Last updated: Jan 25 '18