i have this below code and want do a subtract such:
final azimuth = altitude every stage running – altitude previous stage.
for example if we are in 4 times of iteration final altitude must be such: altitude 4 times – altitude 3 times.
i ask AI many questions but the results are same in while the altitude in every stage are changing due to changes in photo and line length. and final altitude and final azimuths are changing.
my codes are:
import time
import numpy as np
import cv2
import serial
from math import atan, sqrt, degrees
# Initialize the serial port
ser = serial.Serial('COM7', baudrate=9600, timeout=1)
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(0, 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(image, 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 largest contour (assuming it's the line)
c = max(contours, key=cv2.contourArea)
# Get the extreme points of the contour (line)
x1, y1 = c[c[:, :, 0].argmin()][0]
x2, y2 = c[c[:, :, 0].argmax()][0]
# Calculate the length of the line
length = sqrt((x2 - x1)**2 + (y2 - y1)**2)
# Calculate azimuth and altitude angles
azimuth = atan((x2 - x1) / (y2 - y1))
altitude = atan(length / 13)
# Convert angles to degrees
solaraltitude = degrees(altitude)
solarazimuth = degrees(azimuth)
# Convert negative azimuth angles to positive
if solarazimuth < 0:
solarazimuth += 360
return x1, y1, x2, y2, solaraltitude, solarazimuth
else:
return None, None, None, None, None, None
N = 7
previous_altitude = 0
previous_azimuth = 0
fix_altitude = 60
fix_azimuth = 45
for i in range(N):
im = captureImage()
result = processImage(im)
if result[0] is not None and result[1] is not None and result[2] is not None and result[3] is not None:
x1, y1, x2, y2, altitude, azimuth = result
if i == N - 1: # Check if it's the last iteration
final_altitude_integer = fix_altitude // 6
final_azimuth_integer = fix_azimuth // 6
else:
if i == 0:
final_altitude = altitude - fix_altitude
final_azimuth = azimuth - fix_azimuth
else:
final_altitude = altitude - previous_altitude
final_azimuth = azimuth - previous_azimuth
final_altitude_integer = int(final_altitude) // 6
final_azimuth_integer = int(final_azimuth) // 6
print("Final altitude (Integer):", final_altitude_integer)
print("Final azimuth (Integer):", final_azimuth_integer)
# Send final altitude and azimuth integers to Arduino via serial
ser.write(f"{final_altitude_integer} {final_azimuth_integer}".encode())
time.sleep(100)
previous_altitude = altitude
previous_azimuth = azimuth
else:
print("No lines detected in the image.")
and results are
`Capturing image
Processing image
Final altitude (Integer): 4
Final azimuth (Integer): -5
Capturing image
Processing image
Final altitude (Integer): 0
Final azimuth (Integer): 0
Capturing image
Processing image
Final altitude (Integer): 0
Final azimuth (Integer): 0
Capturing image
Processing image
Final altitude (Integer): 0
Final azimuth (Integer): 0
Capturing image
Processing image
Final altitude (Integer): 0
Final azimuth (Integer): 0`
i do not accept these results because the altitudes are changing in every stage.
how i can fix these problems thanks.
isa davoud is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.