I am writing a program to capture image using a webcam, can find the largest contour and plot it to an app using pyqtgraph plotwidget .
I am capturing a frame using opencv from the webcam, attached on my xps 13. Then I am trying to display it with embedded qt ui using plotwidget (I have promoted in the designer to plotwidget I tried using ImageView, but it gives me error when I try to to setUI).
My current problem is and I have been searching the solution for 3 days now is that the image captured is displayed outside the bounds of plotwidget. all I can see is a cropped image in the plotwidget.
I tried setting up a separate viewbox and add it as a setcentralwidget() to plotwidget. I tried setting properties of plotwidget directly such as enablerange. etc. I am at my wits end and looking for anyone to help.
class WebcamWorker(QRunnable):
def __init__(self, cam,width,height,fps):
super(WebcamWorker, self).__init__()
# Store constructor arguments (re-used for processing)
self.cam = cam
self.width = width
self.height = height
self.fps = fps
self.signals = WebCamSignal()
self.capture = cv2.VideoCapture(self.cam)
self.capture.set(cv2.CAP_PROP_FRAME_WIDTH, self.width)
self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT, self.height)
self.capture.set(cv2.CAP_PROP_FPS, self.fps)
@pyqtSlot()
def run(self):
#etval, img = self.capture.read(0)
while(running):
retval, img = self.capture.read(0)
cv2.waitKey(30)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
ret, thresh = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 100, 255, cv2.THRESH_BINARY_INV)
#blur = cv2.GaussianBlur(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), (5, 5), 0)
#ret3, thresh = c(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
x=[]
y=[]
w=[]
h=[]
area_c=[]
if len(contours) >0:
for c in contours:
#cnt = contours[0]
x_tmp, y_tmp, w_tmp, h_tmp = cv2.boundingRect(c)
x.append(x_tmp)
y.append(y_tmp)
w.append(w_tmp)
h.append(h_tmp)
area_c.append(cv2.contourArea(c))
#if len(self.area_c) > 0:
index = area_c.index(max(area_c))
#cv2.rectangle(img, (x_tmp, y_tmp), (x_tmp + w_tmp, y_tmp + h_tmp), (255, 153, 0), 1)
cv2.rectangle(img, (x[index], y[index]), (x[index] + w[index], y[index] + h[index]), (50, 255, 255), 1)
X0 = (x[index]/self.width)*100
Y0 = (y[index]/self.height)*100
#x,y corners of the rectangle
cv2.putText(img,f'{X0:.2f}% , {Y0:.2f}%',(x[index]-50,y[index]),cv2.FONT_HERSHEY_SIMPLEX,0.4,(255,255,255),1,cv2.LINE_AA)
#height of rectangle (height)
cv2.putText(img, str(w[index]), (x[index]+(int(w[index]/2)), y[index]),
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1, cv2.LINE_AA)
#width of the rectangle
cv2.putText(img, str(h[index]), (x[index]-50, y[index]+(int(h[index]/2))),
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1, cv2.LINE_AA)
(h_max, w_max) = img.shape[:2]
# calculate the center of the image
center = (w_max / 2, h_max / 2)
# angle90 = 90
angle180 = 180
angle270 = 270
scale = 1.0
M = cv2.getRotationMatrix2D(center, 270, scale)
img = cv2.warpAffine(img, M, (h_max, w_max))
self.signals.captured.emit(img)
2. create instance of class:
3. main ui from designer .ui file
4. display image to plotwidget