import cv2
import numpy as np
def countIOU(recA,recB):
xA=max(recA[0],recB[0])
yA=max(recA[1],recB[1])
xB=min(recA[2],recB[2])
yB=min(recA[3],recB[3])
interarea=max(0,xB-xA)*max(0,yB-yA)
recA_area=(recA[2]-recA[0])*(recA[3]-recA[1])
recB_area=(recB[2]-recB[0])*(recB[3]-recB[1])
return interarea/float(recA_area+recB_area-interarea)
img=np.zeros((512,512,3),np.uint8)
img.fill(255)
recA=[50,50,300,300]
recB=[60,60,320,320]
cv2.rectangle(img,(recA[0],recA[1]),(recA[2],recA[3]),(0,255,0),5)
cv2.rectangle(img,(recB[0],recB[1]),(recB[2],recB[3]),(255,0,0),5)
IOU=countIOU(recA,recB)
font=0
cv2.putText(img,"IOU=%.2f"%IOU,(130,190),font,0.8,(0,0,0),2)
cv2.imshow("image",img)
cv2.waitKey()
cv2.destoryallwindows()
import cv2
import numpy as np
def IOU(rec1,rec2):
x1=max(rec1[0],rec2[0])
y1=max(rec1[1],rec2[1])
x2=min(rec1[2],rec2[2])
y2=min(rec1[3],rec2[3])
iou=max(0,x2-x1)*max(0,y2-y1)
area1=(rec1[2]-rec1[0])*(rec1[3]-rec1[1])
area2=(rec2[2]-rec2[0])*(rec2[3]-rec2[1])
return iou/(area2+area1-iou)
img=np.zeros((512,512,3),np.uint8)
img.fill(255)
rec1=[50,50,300,300]
rec2=[60,60,320,320]
cv2.rectangle(img,(rec1[0],rec1[1]),(rec1[2],rec1[3]),(0,255,0),5)
cv2.rectangle(img,(rec2[0],rec2[1]),(rec2[2],rec2[3]),(0,0,255),5)
font=0
cv2.putText(img,"IOU=%.2f"%IOU(rec1,rec2),(150,200),2,0.8,(0,0,0),2)
cv2.imshow("image",img)
cv2.waitKey()
def IOU(rec3,rec4):
x1=max(rec3[0],rec4[0])
y1=max(rec3[1],rec4[1])
x2=min(rec3[2],rec4[2])
y2=min(rec3[3],rec4[3])
interarea=max(0,x2-x1)*max(0,y2-y1)
area1=(rec3[2]-rec3[0])*(rec3[3]-rec3[1])
area2=(rec4[2]-rec4[0])*(rec4[3]-rec4[1])
return interarea/(area1+area2-interarea)
import cv2
import numpy as np
img=np.zeros((500,500,3),np.uint8)
img.fill(255)
rec1=[50,50,300,300]
rec2=[60,60,320,320]
cv2.rectangle(img,(rec1[0],rec1[1]),(rec1[2],rec1[3]),(0,255,0),5)
cv2.rectangle(img,(rec2[0],rec2[1]),(rec2[2],rec2[3]),(0,0,255),5)
font=0
cv2.putText(img,"IOU=%.2f"%IOU(rec1,rec2),(200,200),font,0.7,(255,0,0),1)
cv2.imshow("img",img)
cv2.waitKey()
def IOU(rec3,rec4):
x1=max(rec3[0],rec4[0])
y1=max(rec3[1],rec4[1])
x2=min(rec3[2],rec4[2])
y2=min(rec3[3],rec4[3])
interarea=max(0,x2-x1)*max(0,y2-y1)
area1=(rec3[2]-rec3[0])*(rec3[3]-rec3[1])
area2=(rec4[2]-rec4[0])*(rec4[3]-rec4[1])
return interarea/(area1+area2-interarea)
import cv2
import numpy as np
img=np.zeros((500,500,3),np.uint8)
img.fill(255)
rec1=[50,50,300,300]
rec2=[60,60,320,320]
cv2.rectangle(img,(rec1[0],rec1[1]),(rec1[2],rec1[3]),(0,255,0),5)
cv2.rectangle(img,(rec2[0],rec2[1]),(rec2[2],rec2[3]),(0,0,255),5)
font=0
cv2.putText(img,"IOU=%.2f"%IOU(rec1,rec2),(200,200),font,0.7,(255,0,0),1)
cv2.imshow("img",img)
cv2.waitKey()
import cv2
import numpy as np
img=np.zeros((500,500,3),np.uint8)
rec1=[50,50,300,300]
rec2=[60,60,320,320]
img.fill(255)
area1=(rec1[2]-rec1[0])*(rec1[3]-rec1[1])
area2=(rec2[2]-rec2[0])*(rec2[3]-rec2[1])
cv2.rectangle(img,(rec1[0],rec1[1]),(rec1[2],rec1[3]),(0,255,0),5)
cv2.rectangle(img,(rec2[0],rec2[1]),(rec2[2],rec2[3]),(0,0,255),5)
x1=max(rec1[0],rec2[0])
y1=max(rec1[1],rec2[1])
x2=min(rec1[2],rec2[2])
y2=min(rec1[3],rec2[3])
IOU=max(0,x2-x1)*max(0,y2-y1)/(area1+area2-max(0,x2-x1)*max(0,y2-y1))
font=0
cv2.putText(img,"IOU=%.2f"%IOU,(200,200),font,0.8,(255,0,0),2)
cv2.imshow("img",img)
cv2.waitKey()