【问题标题】:ROI is written in lighter colors than original pictureROI 以比原始图片更浅的颜色书写
【发布时间】:2017-03-06 11:59:21
【问题描述】:

我正在尝试在图片上定位对象(此处为 PWB)。 首先,我通过找到最大的轮廓来做到这一点。然后我想只把这个对象改写成一张新图片,以便将来我可以处理更小的图片。 然而问题是,当我重写这个 ROI 时,图片的颜色比原来的颜色要浅。

代码:

Original = cv2.imread(picture_location)
image = cv2.imread(mask_location)
img = cv2.medianBlur(image,29)
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dst = cv2.bitwise_and(Original, image)
roi = cv2.add(dst, Original)
ret,thresh = cv2.threshold(imgray,127,255,0)
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
area = 0
max_x = 0
max_y = 0
min_x = Original.shape[1]
min_y = Original.shape[0]
for i in contours:
    new_area = cv2.contourArea(i)
    if new_area > area:
        area = new_area
        cnt = i
x,y,w,h = cv2.boundingRect(cnt)
min_x = min(x, min_x)
min_y = min(y, min_y)
max_x = max(x+w, max_x)
max_y = max(y+h, max_y)
roi = roi[min_y-10:max_y+10, min_x-10:max_x+10]
Original = cv2.rectangle(Original,(x-10,y-10),(x+w+10,y+h+10),(0,255,0),2)

#Writing down the images
cv2.imwrite('Pictures/PCB1/LocatedPCB.jpg', roi)
cv2.imwrite('Pictures/PCB1/LocatedPCBContour.jpg',Original)

由于我还没有 10 声望,所以我无法发布图片。但是我可以提供链接:

原创 感兴趣的地区

主要问题是如何让软件以与原始图片完全相同的颜色写下 ROI? 但是,我是一名机电工程师,所以我对此还很陌生,如果可能的话,我也会感谢您对我编写代码的方式发表评论。

【问题讨论】:

    标签: image python-2.7 opencv roi


    【解决方案1】:

    问题是,你先让roi = cv2.add(dst, Original) ,最后从这里的变亮图片中剪下来:

    roi = roi[min_y-10:max_y+10, min_x-10:max_x+10]
    

    如果你想裁剪原始图像,你应该这样做:

    roi = Original[min_y-10:max_y+10, min_x-10:max_x+10]
    

    【讨论】:

      【解决方案2】:

      您或许可以在图像模糊后执行边缘检测。

      如何为 Canny edge 选择最佳参数? SEE HERE

      lower = 46
      upper = 93
      edged = cv2.Canny(img, lower, upper) #--- Perform canny edge on the blurred image
      
      kernel = np.ones((5,5),np.uint8)
      dilate = cv2.morphologyEx(edged, cv2.MORPH_DILATE, kernel, 3) #---Morphological dilation
      
      _, contours , _= cv2.findContours(dilate, cv2.RETR_EXTERNAL, 1) #---Finds all parent contours, does not find child contours(i.e; does not consider contours within another contour)
      
      max = 0
      cc = 0
      for i in range(len(contours)):   #---For loop for finding contour with maximum area
          if (cv2.contourArea(contours[i]) > max):
              max = cv2.contourArea(contours[i])
              cc = i
      
      cv2.drawContours(img, contours[cc], -1, (0,255,0), 2) #---Draw contour having the maximum area
      cv2.imshow(Contour of PCB.',img)
      

      x,y,w,h = cv2.boundingRect(cnt[cc])   #---Calibrates a straight rectangle for the contour of max. area
      

      crop_img = img1[y:y+h, x:x+w] #--- Cropping the ROI having the coordinates of the bounding rectangle
      cv2.imshow('cropped PCB.jpg',crop_img)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-12
        • 2012-06-04
        相关资源
        最近更新 更多