【问题标题】:Opencv unable to get ROI of highest intensity partOpencv 无法获得最高强度部分的 ROI
【发布时间】:2015-05-27 10:50:19
【问题描述】:

我已经尝试过这段代码。

import sys
import numpy as np
sys.path.append('/usr/local/lib/python2.7/site-packages')
import cv2
from cv2.cv import *
img=cv2.imread("test2.jpg",cv2.IMREAD_COLOR)
gimg = cv2.imread("test2.jpg",cv2.IMREAD_GRAYSCALE)
b,g,r = cv2.split(img)
ret,thresh1 = cv2.threshold(gimg,127,255,cv2.THRESH_BINARY);
numrows = len(thresh1)    
numcols = len(thresh1[0])
thresh = 170
im_bw = cv2.threshold(gimg, thresh, 255, cv2.THRESH_BINARY)[1]
trig=0
trigmax=0;
xmax=0
ymax=0
for x in range(0,numrows):
for y in range(0,numcols):
    if(im_bw[x][y]==1):
        trig=trig+1;
if(trig>5):
    xmax=x;
    ymax=y;
    break;
print x,y,numrows,numcols,trig
roi=gimg[xmax:xmax+200,ymax-500:ymax]
cv2.imshow("roi",roi)
WaitKey(0)

这里test2.jpg 是我要做的就是专注于图像的高强度部分(即图像中高强度的圆圈)。但我的代码似乎没有这样做。 有人可以帮忙吗?

【问题讨论】:

    标签: python opencv image-processing roi


    【解决方案1】:

    我从here 找到了我的问题的答案

    这是我的代码

    # import the necessary packages
      import numpy as np
      import cv2
    # load the image and convert it to grayscale
     image = cv2.imread('test2.jpg')
     orig = image.copy()
     gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # apply a Gaussian blur to the image then find the brightest
    # region
    gray = cv2.GaussianBlur(gray, (41, 41), 0)
    (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
    image = orig.copy()
    roi=image[maxLoc[1]-250:maxLoc[1]+250,maxLoc[0]-250:maxLoc[0]+250,2:2] 
    cv2.imshow("Robust", roi)
    cv2.waitKey(0)
    

    test2.jpg

    投资回报率

    【讨论】:

      【解决方案2】:

      尝试检查一个像素是否不为零 - 事实证明,这些像素在阈值处理后的值为 255,因为它毕竟是灰度图像。

      阈值似乎也是错误的,但我真的不知道你想看到什么(用 imshow 显示它 - 它不仅仅是圆圈)。并且您的代码与左下角的数字“3”匹配,因此 ROI 矩阵索引在您的示例中无效。

      编辑:

      在玩弄了图像之后,我最终使用了不同的方法。我之前使用了 SimpleBlobDetector 并对图像进行了腐蚀,因此您感兴趣的区域保持连接。对于斑点检测器,程序首先反转图像。 (您可能想像我一样阅读SimpleBlobDetector tutorial,部分代码基于该页面 - 非常感谢作者!)

      下面的代码一步一步的展示了这个过程:

      import cv2
      import numpy as np
      
      # Read image
      gimg = cv2.imread("test2.jpg", cv2.IMREAD_GRAYSCALE)
      
      # Invert the image
      im_inv = 255 - gimg
      cv2.imshow("Step 1 - inverted image", im_inv)
      cv2.waitKey(0)
      
      # display at a threshold level of 50
      thresh = 45
      im_bw = cv2.threshold(im_inv, thresh, 255, cv2.THRESH_BINARY)[1]
      
      cv2.imshow("Step 2 - bw threshold", im_bw)
      cv2.waitKey(0)
      
      # erosion
      kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(10,10))
      im_bw = cv2.erode(im_bw, kernel, iterations = 1)
      
      cv2.imshow('Step 3 - erosion connects disconnected parts', im_bw)
      cv2.waitKey(0)
      
      # Set up the detector with default parameters.
      params = cv2.SimpleBlobDetector_Params()
      params.filterByInertia = False
      params.filterByConvexity = False
      params.filterByCircularity = False
      params.filterByColor = False
      params.minThreshold = 0
      params.maxThreshold = 50
      params.filterByArea = True
      
      params.minArea = 1000    # you may check with 10 --> finds number '3' also
      params.maxArea = 100000 #im_bw.shape[0] * im_bw.shape[1] # max limit: image size
      
      # Create a detector with the parameters
      ver = (cv2.__version__).split('.')
      if int(ver[0]) < 3 :
          detector = cv2.SimpleBlobDetector(params)
      else : 
          detector = cv2.SimpleBlobDetector_create(params)
      
      # Detect blobs.
      keypoints = detector.detect(im_bw)
      print "Found", len(keypoints), "blobs:"
      for kpt in keypoints:
          print "(%.1f, %.1f) diameter: %.1f" % (kpt.pt[0], kpt.pt[1], kpt.size)
      
      # Draw detected blobs as red circles.
      # cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the 
      # circle corresponds to the size of blob
      im_with_keypoints = cv2.drawKeypoints(gimg, keypoints, np.array([]), (0,0,255), 
          cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
      
      # Show keypoints
      cv2.imshow("Keypoints", im_with_keypoints)
      cv2.waitKey(0)
      

      这个算法找到坐标 (454, 377) 作为 blob 的中心,但是如果你将 minArea 减少到例如10 然后它也会在底角找到数字 3。

      【讨论】:

      • 我尝试了非零但没有运气。我要集中精力的是 thislink 。如果我只接受那个 thresh,那么我的集中区域将是白色,而其他区域将是黑色的。所以我在想白色区域的值为 1,深色区域的值为 0。你能告诉我哪里错了吗?
      • @SGiritheja 白色区域在您的灰度图像中的值为 255(在阈值之后),因此与 1 而不是 255 进行比较是错误的。如果您按照您在示例中编写的顺序检查像素,您将在图像的底部找到数字“3”。我建议先显示 im_bw 看看你在做什么。
      • 我在我的电脑上尝试了这个程序,程序显示它找到零个斑点并退出。
      • 你能告诉我在运行原始 test2.jpg 的不同步骤中感兴趣的区域吗?对我来说here they are 为简单起见一起复制。 (您可能会看到斑点检测器提供的直径不正确。)
      • (我认为这与您在原始问题中链接的图像不同。)减少 minArea 限制或更改阈值可以修复该图像,但您的解决方案似乎更健壮和直接。你可以用“......最高强度部分”扩展问题标题吗?我什至没有意识到它在最后一句中......
      猜你喜欢
      • 1970-01-01
      • 2019-04-30
      • 2015-06-13
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      • 2016-07-11
      • 1970-01-01
      相关资源
      最近更新 更多