小编Han*_*rse的帖子

如何在OpenCV中进行卷积

我试图使用filter2D方法在OpenCV中找到卷积,但结果不正确

import cv2 as cv
import scipy.signal as sig
import numpy as np
b=np.asarray([[1,2,0,1,2],
              [2,3,1,1,2],
              [1,4,2,2,0],
              [3,2,3,3,0],
              [1,0,0,2,1]
              ],dtype=np.uint8)

w=np.asarray([[1,1,1],
              [1,1,2],
              [2,1,1]],dtype=np.uint8)
w_r=np.asarray([[1,1,1],
                [2,1,1],
                [1,1,1]
                ],dtype=np.uint8)
print(sig.convolve2d(b,w,mode="same"))
kernel_r=np.asarray([[1,1,1],[1,1,2],[2,1,1]])
print("-------")
print(cv.filter2D(b,-1,w_r))
Run Code Online (Sandbox Code Playgroud)

第一个输出是由 scipy.signal.convolve2D 生成的,这是正确的。第二个输出是由 OpenCV filter2D 生成的,这是不正确的。我怎样才能得到正确的结果。

[[ 8 10 10  7  7]
 [15 18 20 14  9]
 [18 23 26 18 10]
 [15 21 22 16 11]
 [ 8 13 13  9  8]]
-------
[[23 16 15 11 13]
 [25 18 19 12 13]
 [28 22 25 16 16]
 [19 …
Run Code Online (Sandbox Code Playgroud)

python opencv convolution

3
推荐指数
1
解决办法
2万
查看次数

如何获取带孔二值掩模的边界坐标?

我有以下图像:

测试图像

我想获得一个列表,其中包含(x, y)每个斑点的外部和内部轮廓的坐标(我们称它们为斑点 A 和 B)。

import cv2
from skimage import measure

blob = cv2.imread('blob.png', 0)
contours, hier = cv2.findContours(blob, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
labels = measure.label(blob)
props = measure.regionprops(labels)

for ii in range(0,len(props))
xy = props[ii].coords

plt.figure(figsize=(18, 16))
plt.imshow(blob, cmap='gray')
plt.plot(xy[:, 0], xy[:,1])
plt.show()
Run Code Online (Sandbox Code Playgroud)

所需的输出图像,其中蓝色和红色是从(x, y)坐标列表 A 和 B 中绘制的:

所需输出

python opencv contour

3
推荐指数
1
解决办法
6596
查看次数

如何将 PNG 图像转换为透明 GIF?

我正在尝试将具有透明背景的 PNG 图像列表转换为 GIF,同时保持背景透明度。我找到了这段代码,并对其进行了修改:

import os
from PIL import Image

# Create the frames
frames = []

path = "directory/to/my/png/images"
for frame in os.listdir(path):
    new_frame = Image.open(path + "/" + frame)
    frames.append(new_frame)

# Save into a GIF file
frames[0].save(path + "/../output/animation.gif", format='GIF',
               append_images=frames[1:],
               save_all=True,
               duration=41, loop=1, transparency=0)
Run Code Online (Sandbox Code Playgroud)

它正在打开文件夹中的所有 PNG 图像,并将它们导出到 GIF,但背景是黑色的。我看过PIL文档,但我似乎不明白该transparency参数是如何工作的,或者我认为我使用错误。

python png transparency gif python-imaging-library

3
推荐指数
1
解决办法
2833
查看次数

黄色车道线的HSL范围

我目前正在进行简单的车道检测,发现黄色车道线的范围/输入值时遇到一些麻烦。

def color_filter(image):
    #convert to HLS to mask based on HLS
    hls = cv2.cvtColor(image, cv2.COLOR_BGR2HLS)
    lower = np.array([0,190,0])
    upper = np.array([255,255,255])

    yellower = np.array([40,70,60]) #NOT SURE WHAT TO PUT
    yelupper = np.array([50,90,65]) #NOT SURE WHAT TO PUT

    yellowmask = cv2.inRange(hls, yellower, yelupper)    
    whitemask = cv2.inRange(hls, lower, upper)

    mask = cv2.bitwise_or(yellowmask, whitemask)  
    masked = cv2.bitwise_and(image, image, mask = mask)    

    return masked
Run Code Online (Sandbox Code Playgroud)

这是我过滤的图像(仅显示白色通道):

http://prntscr.com/ng2cgp

这是原始图片:

http://prntscr.com/ng2cx6

python opencv colors python-3.x opencv3.0

2
推荐指数
1
解决办法
147
查看次数

如何求两组3D点之间的仿射变换矩阵?

我需要注册为视频的每一帧提供的一些 3D 面部标志。对于此任务,我试图找出为连续帧给出的几个地标坐标之间的变换矩阵。例如,第 1 帧和第 2 帧中 3 个地标的 3D 坐标如下:

frame1 = [2 4 15; 4 15 14; 20 11 7]
frame2 = [16 5 12; 5 7 9; 11 6 19]
Run Code Online (Sandbox Code Playgroud)

我尝试过使用imregtformmatlab提供的函数和matlab的ABSOR工具。

tform = imregtform(frame1, frame2, 'affine','OnePlusOneEvolutionary','MeanSquares');

tform = absor(frame1, frame2)
Run Code Online (Sandbox Code Playgroud)

使用时出现如下错误imregtform

Error using imregtform>parseInputs (line 261)
The value of 'MovingImage' is invalid. All dimensions of the moving image should be greater than 4.

Error in imregtform (line 124)
parsedInputs = parseInputs(varargin{:});
Run Code Online (Sandbox Code Playgroud)

注意:ABSOR 不查找仿射变换,它查找相似变换。

matlab affinetransform

2
推荐指数
1
解决办法
7371
查看次数

从图像中删除抗锯齿功能

我想从图像中删除抗锯齿功能。此代码将从图像中获取 4 种主要颜色,将每个像素与 4 种主要颜色进行比较并分配最接近的颜色。

import numpy as np
from PIL import Image

image = Image.open('pattern_2.png')
image_nd = np.array(image)
image_colors = {}

for row in image_nd:
    for pxl in row:
        pxl = tuple(pxl)
        if not image_colors.get(pxl):
            image_colors[pxl] = 1
        else:
            image_colors[pxl] += 1

sorted_image_colors = sorted(image_colors, key=image_colors.get, reverse=True)
four_major_colors = sorted_image_colors[:4]


def closest(colors, color):
    colors = np.array(colors)
    color = np.array(color)
    distances = np.sqrt(np.sum((colors - color) ** 2, axis=1))
    index_of_smallest = np.where(distances == np.amin(distances))
    smallest_distance = colors[index_of_smallest]
    return smallest_distance[0]


for y, …
Run Code Online (Sandbox Code Playgroud)

python numpy image-processing antialiasing python-imaging-library

2
推荐指数
1
解决办法
2850
查看次数

轮廓的 arcLength 的结果是什么?

我在 Google 中搜索arcLength,也许我能理解它,但是它如何在 EmguCV 或 OpenCV 中处理图像中的轮廓呢?我尝试使用 MATLAB 制作一个小图像。图像是9 x 9,我在图像中画了一条线,该线是 1 像素。我在 EmguCV 中使用此代码来检测轮廓:

VectorOfVectorOfPoint cons = new VectorOfVectorOfPoint();

        CvInvoke.FindContours(img_gray, cons, null, RetrType.List, ChainApproxMethod.ChainApproxNone);
        for(int i=0; i<cons.Size;i++)
        {
            VectorOfPoint points = cons[i];
            for(int x =0; x<points.Size;x++)
            {
               temp[points[x]] = new Gray(255);
            }
           double c= CvInvoke.ArcLength(cons[i], true);
            textBox1.Text = c.ToString();             
        }

        imageBox2.Image = temp;
Run Code Online (Sandbox Code Playgroud)

arcLength曾是:

  • 当线条为 1 像素时 ->arcLength为 0。
  • 当线条为 2 个像素时 ->arcLength为 2。
  • 当线条为 3 像素时 ->arcLength为 …

c# opencv emgucv

2
推荐指数
1
解决办法
5941
查看次数

如何提高该图像的 OCR 准确性?

我将使用 Python 中的 OpenCV 和 OCR by 来从图片中提取文本pytesseract。我有这样的图像:

输入

然后我编写了一些代码来从该图片中提取文本,但它没有足够的精度来正确提取文本。

这是我的代码:

import cv2
import pytesseract
    
img = cv2.imread('photo.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_,img = cv2.threshold(img,110,255,cv2.THRESH_BINARY)

custom_config = r'--oem 3 --psm 6'
text = pytesseract.image_to_string(img, config=custom_config)
print(text)

cv2.imshow('pic', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)

我已经测试过cv2.adaptiveThreshold,但它不起作用cv2.threshold

最后,这是我的结果,与图片中的结果不同:

Color Yellow RBC/hpf 4-6
Appereance Semi Turbid WBC/hpf 2-3
Specific Gravity 1014 Epithelial cells/Lpf 1-2
PH 7 Bacteria (Few)
Protein Pos(+) Casts Negative
Glucose Negative Mucous (Few)
Keton Negative
Blood Pos(+)
Bilirubin Negative …
Run Code Online (Sandbox Code Playgroud)

python ocr opencv image-processing python-tesseract

2
推荐指数
1
解决办法
3213
查看次数

如何删除图像中的外圆而不影响图像的其余部分?

我有一个类似于下面显示的图像的图像。 在此处输入图片说明

我想删除图像的黑色和红色圆圈,而又不影响图像内部的红色方块(因为红色圆圈和红色方块的像素值相同)。

我尝试使用cv2.HoughCircles检测红色圆圈并将其转换为黑色,但红色圆圈的某些部分保持不变,如图所示。

在此处输入图片说明

这是我用于此的代码。

import numpy as np
import cv2

image = cv2.imread("13-14.png")
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.3, 145)

if circles is not None:
    circles = np.round(circles[0, :]).astype("int")

    for (x, y, r) in circles:
        cv2.circle(output, (x, y), r, (0, 0 , 0), 4)

cv2.imshow("output", np.hstack([image, output]))
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)

有什么建议么?提前致谢。

编辑1

我正在寻找的样本输出就是这种图像(彩色或灰度)。

在此处输入图片说明

python opencv image-processing python-3.x

1
推荐指数
1
解决办法
64
查看次数

匿名函数是否支持可选参数?

有没有办法在 MATLAB 中实现的匿名函数中使用可选参数?

请参阅以下示例:

foo = @(x,y)(x+y+12)
Run Code Online (Sandbox Code Playgroud)

可以y是上述匿名函数中的可选参数,例如

foo = @(x,y?)(x+y+12)
Run Code Online (Sandbox Code Playgroud)

并且仅y在提供时使用?

matlab anonymous-function optional-parameters

1
推荐指数
1
解决办法
75
查看次数