I have removed non maxima regions from the vector "bbox" . But when I plot it shows the regions which still contain non maxima regions. If I measure the bbox.size() before & after non max suppression it says 4677 & 3582 respectively.Why its not able to remove all the non maximas ??
I have taken area and the start and end x & y co ordinates as the criterion.
Complete code is here: https://stackoverflow.com/questions/44537429/how-to-plot-the-non-maxima-supressed-regions-opencv-c?noredirect=1#comment76067794_44537429
Note: The same method in python yields much lesser regions and suppresses it all. Is there a difference between detectRegions in python & c++ ??
C++ :
Mat src = imread("/home/clabs/MANU/right_side/53.jpg",CV_LOAD_IMAGE_UNCHANGED);
Mat gray;
Mat src1 = src.clone();
cvtColor(src, gray, CV_RGB2GRAY);
Ptr<MSER> ms = MSER::create();
vector<Rect> bbox,filtered;
vector<vector<Point> > regions;
vector <int> vec;
ms->detectRegions(gray,regions,bbox);
for (int k = 0; k < bbox.size(); k++)
{
rectangle(src,bbox[k], CV_RGB(0, 255, 0));
}
imwrite("Unsupressed.jpg",src);
cout<<bbox.size()<<"Befor Delete"<<endl;
for (int i = 0; i<bbox.size(); i++){
for(int j = 0; j<bbox.size(); j++){
if( (bbox[i].x > bbox[j].x) && (bbox[i].y > bbox[j].y) && ((bbox[i].x+bbox[i].width) < (bbox[j].x+bbox[j].width)) && ((bbox[i].y+bbox[i].height) < (bbox[j].y+bbox[j].height)) && (bbox[i].area() < bbox[j].area())){
bbox.erase(bbox.begin() + i );
}
}
}
cout<<bbox.size()<<" After"<<endl;
cout<<vec.size()<<endl;
for (int k = 0; k < bbox.size(); k++)
{
rectangle(src1,bbox[k], CV_RGB(255,0,0));
//cout<<k<<endl;
}
imshow("mser", src1);
imwrite("NoN.jpg",src1);
waitKey(0);
return 0;
}
Python code:
import cv2
import numpy as np
img = cv2.imread('/home/cocoslabs/selimspp/12.jpg')
mser = cv2.MSER_create()
mser = cv2.MSER()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #Converting to GrayScale
gray_img = img.copy()
regions = mser.detectRegions(gray, None)
regions = mser.detect(gray, None)
print regions
xsminr=[]
ysminr=[]
xsmaxr=[]
ysmaxr=[]
wsr=[]
hsr=[]
arsr=[]
for r in regions:
xsr=[]
ysr=[]
for c in r:
xsr.append(c[0])
ysr.append(c[1])
xsminr.append(min(xsr))
xsmaxr.append(max(xsr))
ysminr.append(min(ysr))
ysmaxr.append(max(ysr))
#txmin=c[0]
#txmax=c[0]
#tymin=c[1]
#tymax=c[1]
for i in range(0,len(xsminr)):
ws=xsmaxr[i]-xsminr[i]
hs=ysmaxr[i]-ysminr[i]
ars=ws*hs
wsr.append(ws)
hsr.append(hs)
arsr.append(ars)
#if(txmin<c[0]): #="" txmin="txmin" #else:="" #="" txmin="c[0]" #if(txmax="">c[0]):
# txmax=txmax
#else:
# txmax=c[0]
# xsminr.append(txmin)
# xsmaxr.append(txmax)
print xsminr
print len(xsminr)
print len(xsmaxr)
print len(ysminr)
print len(ysmaxr)
print max(xsminr)
print max(xsmaxr)
print max(ysminr)
print max(ysmaxr)
hthresh=40
wthresh=100
hmaxthresh=40
index=[]
indexlar=[]
for i in range(0,len(xsminr)):
if(hsr[i]>hmaxthresh):
indexlar.append(i)
xsminr=np.delete(xsminr,indexlar)
ysminr=np.delete(ysminr,indexlar)
xsmaxr=np.delete(xsmaxr,indexlar)
ysmaxr=np.delete(ysmaxr,indexlar)
for i in range(0,len(xsminr)):
for j in range(0,len(xsminr)):
if(hsr[j]<hthresh): if((xsminr[i]>="xsminr[j])" &="" (ysminr[i]>="ysminr[j])" &="" (xsmaxr[i]<="xsmaxr[j])" &="" (ysmaxr[i]<="ysmaxr[j])):" if(arsr[i]<arsr[j]):="" if((i="" in="" index)="=0):" <br="">
index.append(i)
# print "entered",j
#print "j",i
xsminr_n=np.delete(xsminr,index)
xsmaxr_n=np.delete(xsmaxr,index)
ysminr_n=np.delete(ysminr,index)
ysmaxr_n=np.delete(ysmaxr,index)
hsr_n=np.delete(hsr,index)
wsr_n=np.delete(wsr,index)
print len(index)
print len(xsminr_n)
print len(xsmaxr_n)
print len(ysminr_n)
print len(ysmaxr_n)
print len(wsr)
print len(hsr)
print hsr
print wsr
for i in range(0,len(xsminr_n)):
if(hsr_n[i]>hmaxthresh):
indexlar.append(i)
xsminr_n=np.delete(xsminr_n,indexlar)
ysminr_n=np.delete(ysminr_n,indexlar)
xsmaxr_n=np.delete(xsmaxr_n,indexlar)
ysmaxr_n=np.delete(ysmaxr_n,indexlar)
for i in range(0,len(xsminr_n)):
#if(wsr[i]<wthresh): #cv2.rectangle(gray_img,(xsminr_n[i],ysminr_n[i]),(xsmaxr_n[i],ysmaxr_n[i]),(0,255,0),1)="" imcrop="gray_img[ysminr_n[i]:ysmaxr_n[i],xsminr_n[i]:xsmaxr_n[i]]" cv2.imwrite("="" home="" cocoslabs="" croppedpp="" 12="" all="" "+str(i)+".jpg",imcrop)<="" p="">
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]
cv2.polylines(gray_img, hulls, 1, (0, 0, 255), 2)
cv2.imwrite('/home/cocoslabs/outm.jpg', gray_img) #Saving
cv2.imshow('result',gray_img)
cv2.waitKey(0)
cv2.destroyAllWindows()