获取线的精确(x,y)坐标[关闭]

问题描述 投票:0回答:1

嗨,好人,我通过 python 进行图像处理并打开 CV。在我的项目中坐标必须相同,因为线不会改变。我的相机和我的线从未移动过。 从图片中拍照。线的坐标(顶部和底部) 我使用极值点和检测到的线来获取顶部和底部线的坐标(顶部为x1,y1,底部为x2,y2)。 但是当我的循环的不同阶段中线的顶部和底部的坐标不相同时,就会出现问题。我的 x1 、 x2 坐标有更多方差,而 y1、y2 对我来说没有挑战就很好。我什至买了一台质量更高的相机,但没什么用。我想优化我的代码以在不同时间获得相同的坐标,特别是在 x1、x2 坐标中。我的代码如下,我的输出是: (83, 250) 至 (92, 256) (177, 253) 至 (180, 256) (231, 252) 至 (238, 256) (186, 253) 至 (188, 256) 正如您所看到的 x1 , x2 比 y1 , y2 具有更大的方差。我想优化x。 感谢您抽出时间并帮助我。欣赏

import time
import numpy as np
import cv2
from math import sqrt

def captureImage():
    print('Capturing image')

    videoCaptureObject = cv2.VideoCapture(1)

    result = True
    while(result):
        ret, frame = videoCaptureObject.read()
        cv2.imwrite("Newpicture.jpg", frame)
        result = False
        videoCaptureObject.release()
        im = np.random.randint(50, 256, size=(256, 256, 3), dtype=np.uint8)
        return im


def processImage(im):
    print('Processing image')
    image = cv2.imread('C:/Users/Stk/Desktop/Newpicture.jpg')

    # Convert image to grayscale
    gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    
    # Apply Gaussian blur to reduce noise
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    
    # Apply edge detection
    edges = cv2.Canny(blurred, 50, 150)
    
    # Find contours in the edge-detected image
    contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    
    if contours:
        # Get the extreme points of the contour (line)
        for contour in contours:
            x, y, w, h = cv2.boundingRect(contour)
            x1, y1 = x, y
            x2, y2 = x + w, y + h
            length = sqrt((x2 - x1)**2 + (y2 - y1)**2)
            
            return x1, y1, x2, y2, length
    else:
        return None, None, None, None, None

N = 4
for i in range(N):
    frame = captureImage()
    x1, y1, x2, y2, length = processImage(frame)
    if x1 is not None:
        print(f"Line coordinates: ({x1}, {y1}) to ({x2}, {y2})")
        print(f"Line length: {length}")
        from cmath import atan
        import math
        azimuth = atan((x1 - x2) / (y1 - y2))
        altitude = atan(length/280)
        solaraltitude = float(altitude.real) * (180.0 / 3.141592653589793238463)  # Convert complex to real
        solarazimuth = float(azimuth.real) * (180.0 / 3.141592653589793238463)
        print(f"Solar altitude: {solaraltitude}")
        print(f"Solar azimuth: {solarazimuth}")
    else:
        print("No line detected in the image.")
    
    time.sleep(5)

我首先使用具有很大方差的HUOGH TRANSFORM LINE,然后使用极值点,它给我更好的坐标,但x坐标也有方差。

python opencv image-processing spyder coordinate-systems
1个回答
2
投票

不知道您到底需要哪个坐标,我可以给您以下代码供您用作“工具”。

首先我们加载图像,你的对象与背景非常不同,我们可以很容易地掩盖它:

im = cv2.imread("contour.jpg", 0) # read as gray mask = (im<40).astype(np.uint8) # get the line thing
此代码导致:

mask

现在我们检测到了对象,我们可以使用以下行获取所有

True

的坐标:

Y, X = np.where(mask == 1) # get the x and y coordinates
您决定如何处理这些坐标取决于您。在这个例子中,我分散了点并拟合了一条线来绘制:

im = cv2.imread("contour.jpg", 0) # read as gray mask = (im<40).astype(np.uint8) # get the line thing Y, X = np.where(mask == 1) # get the x and y coordinates fit = np.polyfit(X, Y, 1) # fit a line plt.figure() # new fig plt.imshow(im) # show im plt.scatter(X, Y, 0.1, color = "r", marker = "x") # scatter the points as red plt.plot(X, np.polyval(fit, X), color = "k") # plot the fitted line plt.axis("off") # remove the axis
这会导致以下图像:

scattering

在这里我们看到图像、真实的点和拟合线。

如果您想使用

thinning,请检查this post以及其中发布的有用链接。如果您需要更多帮助,请在下面评论。

© www.soinside.com 2019 - 2024. All rights reserved.