我在使用 OpenCV 和 Python 时遇到问题,我是这项技术的新手。只是一些问题,应用霍夫线变换后如何裁剪图像?
这是我的形象。我想用那些有红线的图像来裁剪图像。
这是我裁剪图像的代码,我知道有问题。
minLineLength = 100
maxLineGap = 10
rho = 1
theta = np.pi/180
threshold = 190
lines = cv2.HoughLines(opened, 1, np.pi/180, threshold)
for line in lines:
for rho,theta in line:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
cv2.line(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
cropped = image[100:200, …Run Code Online (Sandbox Code Playgroud) 我有一个小脚本(GitHub)(基于这个答案)来检测白色背景上的对象。该脚本工作正常并检测到对象。例如,这张图片:
变成这样:
然后我裁剪boundingRect(红色的)。
我将对这张图片进行进一步的操作。例如,我将只裁剪轮廓,而不是矩形裁剪。(无论如何,这些都是要面对的进一步问题。)
现在我想做的是放大/增长轮廓(绿色)。我不确定在这种情况下规模和增长是否意味着相同的事情,因为当我想到规模时,通常有一个原点/锚点。对于增长,它是相对于边缘的。我想要这样的东西(在 Photoshop 中创建):
因此,在检测到对象/找到轮廓后,我想按某个值/比率来增长它,以便我有一些空间/像素可以修改,这不会影响对象。我怎样才能做到这一点?
提到的脚本:
# drop an image on this script file
img_path = Path(sys.argv[1])
# open image with Pillow and convert it to RGB if the image is CMYK
img = Image.open(str(img_path))
if img.mode == "CMYK":
img = ImageCms.profileToProfile(img, "Color Profiles\\USWebCoatedSWOP.icc", "Color Profiles\\sRGB_Color_Space_Profile.icm", outputMode="RGB")
img = cv2.cvtColor(numpy.array(img), cv2.COLOR_RGB2BGR)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
threshed = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY_INV)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11,11))
morphed = cv2.morphologyEx(threshed, cv2.MORPH_CLOSE, kernel)
contours …Run Code Online (Sandbox Code Playgroud) 例如,如果我这样做:
cv2.getTextSize('blahblah', cv2.FONT_HERSHEY_SIMPLEX, 2, 2)
它会返回((262, 43), 19)
所以文本的宽度和高度(以像素为单位)分别是 262 和 43,但是 19 是多少?
这里它说它“对应于相对于文本底部的基线的 y 坐标”,但这仍然让我不清楚,因为我不确定这里的“基线”是什么?
我看到很多文献中他们说通过使用fft可以达到更快的卷积.我知道一个人需要获得fft然后从结果中获取,但我真的不明白为什么使用fft可以使卷积更快?
我有细胞的二值图像。我想使用 python 来单独分离这些单元格。每个单元格将保存在图像中。例如,我有 1000 个单元格,那么输出将是 1000 个图像,每个图像包含 1 个单元格。目前我使用两种方式获取但是输出都是错误的
from skimage.morphology import watershed
from skimage.feature import peak_local_max
from skimage import morphology
import numpy as np
import cv2
from scipy import ndimage
from skimage import segmentation
image=cv2.imread('/home/toanhoi/Downloads/nuclei/9261_500_f00020_mask.png',0)
image=image[300:600,600:900]
# First way: peak_local_max
distance = ndimage.distance_transform_edt(image)
local_maxi = peak_local_max(distance, indices=False, footprint=np.ones((3, 3)), labels=image)
markers = morphology.label(local_maxi)
labels_ws = watershed(-distance, markers, mask=image)
markers[~image] = -1
labels_rw = segmentation.random_walker(image, markers)
cv2.imshow('watershed',labels_rw)
cv2.waitKey(5000)
# Second way way: using contour
_,contours,heirarchy=cv2.findContours(image,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(image,contours,-1,(125,125,0),1)
cv2.imshow('contours',image)
cv2.waitKey(5000)
Run Code Online (Sandbox Code Playgroud)
我有这个图像:
我想做的是检测其内部轮廓(数字 3)的质心。
这是我现在的代码:
import cv2
import numpy as np
im = cv2.imread("three.png")
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnts = cv2.drawContours(im, contours[1], -1, (0, 255, 0), 1)
cv2.imshow('number_cnts', cnts)
cv2.imwrite('number_cnts.png', cnts)
m = cv2.moments(cnts[0])
cx = int(m["m10"] / m["m00"])
cy = int(m["m01"] / m["m00"])
cv2.circle(im, (cx, cy), 1, (0, 0, 255), 3)
cv2.imshow('center_of_mass', im)
cv2.waitKey(0)
cv2.imwrite('center_of_mass.png', cnts)
Run Code Online (Sandbox Code Playgroud)
这是(错误的..)结果:
为什么质心被绘制在图像的左侧而不是(或多或少)中心?
有什么办法解决这个问题吗?
我正在关注这个项目,制作一个可以玩 Google Chrome Dino 游戏的 AI。我在捕获屏幕源以生成训练数据时陷入了困境。我是简历新手
该项目应该中断视频源并将 CSV 文件保存在 cv2.waitKey(25) & 0xFF == ord('q'): 条件中。我相信是当按下“q”键时。但是当我按“q”时什么也没有发生。当我按 q 时,此 if 条件中的打印语句不会打印。
另外,虽然控制台在按下“向上”、“向下”或“t”键的情况下打印打印语句,但
cv2.imwrite('./images/frame_(0).jpg'.format(x), img)
Run Code Online (Sandbox Code Playgroud)
似乎不起作用,因为图像文件夹中没有保存图像。
这是代码
import cv2
from mss import mss
import numpy as np
import keyboard
#Captures dinasour run for given coordinates
def start():
"""
Capture video feed frame by frame, crops out coods and the dino then process
"""
sct = mss()
coordinates = {
'top': 168,
'left': 230,
'width': 624,
'height': 141
}
with open('actions.csv','w') as …Run Code Online (Sandbox Code Playgroud) 我有图片 
我正在寻找 python 解决方案,根据图像中的轮廓将该图像中的形状分解成更小的部分。
我在 OpenCV 中研究了 Canny 和 findContours 的解决方案,但它们都不适合我。
使用的代码:
import cv2 import numpy as np
img = cv2.imread('area_of_blob_maxcontrast_white.jpg') edges = cv2.Canny(img, 100, 200)
cv2.imwrite('area_of_blob_maxcontrast_white_edges.jpg',edges)
Run Code Online (Sandbox Code Playgroud)
import numpy as np
import argparse
import cv2
image = cv2.imread('area_of_blob_maxcontrast_white.png')
lower = np.array([0, 0, 0]) upper = np.array([15, 15, 15]) shapeMask = cv2.inRange(image, lower, upper)
(_,cnts, _) = cv2.findContours(shapeMask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE) print "I found %d black shapes" % (len(cnts)) cv2.imshow("Mask", shapeMask)
for c in cnts: …Run Code Online (Sandbox Code Playgroud) 我想使用 Python OpenCV 在图像上应用收缩/凸出滤镜。结果应该是这个例子的某种形式:
\nhttps://pixijs.io/pixi-filters/tools/screenshots/dist/bulge-pinch.gif
\n我已阅读以下 stackoverflow 帖子,该帖子应该是过滤器的正确公式:Barrel/Pincushion 失真的公式
\n但我正在努力在 Python OpenCV 中实现这一点。
\n我读过有关在图像上应用过滤器的地图:Distortion e\xef\xac\x80ect using OpenCv-python
\n根据我的理解,代码可能如下所示:
\nimport numpy as np\nimport cv2 as cv\n\nf_img = \'example.jpg\'\nim_cv = cv.imread(f_img)\n\n# grab the dimensions of the image\n(h, w, _) = im_cv.shape\n\n# set up the x and y maps as float32\nflex_x = np.zeros((h, w), np.float32)\nflex_y = np.zeros((h, w), np.float32)\n\n# create map with the barrel pincushion distortion formula\nfor y in range(h):\n for x in range(w):\n flex_x[y, …Run Code Online (Sandbox Code Playgroud) opencv ×9
python ×8
contour ×2
image ×2
python-3.x ×2
convolution ×1
coordinates ×1
distortion ×1
fft ×1
filter ×1
scale ×1
scikit-image ×1
transform ×1