hi nice people i do a image processing by python and open CV.in my project coordinates must be same because the line do not change. my camera and my line never moved.
take photo from a picture.coordinate of line(top and down)
i use extreme point and detected line to get coordinate of top and down line(x1,y1 for top and x2,y2 for down).
but problem arrive when the coordinate of top and down of line are not same in different stages in my loop. i have more variance in x1 , x2 coordinate and y1, y2 are good without challenge for me. i even buy a more quality camera but no useful. i want optimize my code to get coordinate same in different times especially in x1, x2 coordinate. my codes is below and my output is:
(83, 250) to (92, 256
(177, 253) to (180, 256)
(231, 252) to (238, 256)
(186, 253) to (188, 256)
as you see x1 , x2 has more variance than y1, y2. i want optimize x.
thanks for putting me time and helping me. appreciate
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)
i first use HUOGH TRANSFORM LINE that has very variance then use extreme point it give me better coordinate but also variance in x coordinate.
isa davoud is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.