Opencv无法获得最高强度部分的ROI

jc3wubiy  于 2023-04-07  发布在  其他
关注(0)|答案(3)|浏览(152)

我试过这个代码。

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是我特灵做的是集中在图像的高强度部分(即图像中具有高强度的圆圈)。但我的代码似乎没有这样做。有人能帮忙吗?

h22fl7wq

h22fl7wq1#

我从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

ROI

xmd2e60i

xmd2e60i2#

试着检查一个像素是否不为零--结果是这些像素在阈值处理后的值为255,因为它毕竟是灰度图像。
阈值似乎也是错误的,但我真的不知道你想看到什么(用imshow显示它-它不仅仅是圆圈)。你的代码匹配左下角的数字'3',因此ROI矩阵索引在你的例子中是无效的。

编辑:

在对图像进行处理之后,我最终使用了一种不同的方法。我使用了SimpleBlobDetector,并在之前对图像进行了侵 eclipse ,因此您感兴趣的区域保持连接。对于blob检测器,程序首先反转图像。(您可能想像我一样阅读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)作为斑点的中心,但是如果将minArea减小到例如10,则它也将在底部角落中找到数字3。

xqkwcwgp

xqkwcwgp3#

我正在使用类似的东西,但我遇到了一些问题,使用上述答案。使用建议的裁剪有一些问题:

image[maxLoc[1]-250:maxLoc[1]+250,maxLoc[0]-250:maxLoc[0]+250]

因为具有最大强度值的索引可以在图像的边缘。因此,-250或+250可能会超出图像形状的索引。如果你想泛化,这可能是一个更好的选择:

img = imread("YourImage.png")
idx_max = np.unravel_index(img.argmax(), img.shape)
size = 250

if idx_max[0] - size <= 0:
    start0 = 0
    end0 = size + (size - idx_max[0])
elif idx_max[0] + size > img.shape[0]:
    start0 = idx_max[0] - ((2*size) - (img.shape[0] - idx_max[0])) 
    end0 = img.shape[0] - 1
else:
    start0 = idx_max[0] - size
    end0 = idx_max[0] + size

if idx_max[1] - size <= 0:
    start1 = 0
    end1 = size + (size - idx_max[1])
elif idx_max[1] + size > im.shape[1]:
    start0 = idx_max[1] - ((2*size) - (img.shape[1] - idx_max[1])) 
    end0 = img.shape[1] - 1
else:
    start1 = idx_max[1] - size
    end1 = idx_max[1] + size

ROI = img[start0:end0, start1:end1]

我希望这对你有帮助:)

相关问题