forked from Abid-67/Fire_Detection_using_Webcam_Spectrometer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec_intVSpix.py
49 lines (38 loc) · 1.38 KB
/
spec_intVSpix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Function to capture an image from the webcam
def capture_image():
cap = cv2.VideoCapture(0) # change the parameter to access your particular webcam
if not cap.isOpened():
print("Error: Could not open webcam.")
return None
ret, frame = cap.read()
cap.release()
if not ret:
print("Error: Could not read frame.")
return None
return frame
# Function to process the captured image to extract the spectrum
def process_image(image):
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Sum the pixel values along the vertical axis to get the intensity distribution
intensity = np.sum(gray, axis=0)
return intensity
# Function to plot the spectrum as intensity vs. pixel position
def plot_spectrum(intensity):
pixel_positions = np.arange(len(intensity))
plt.plot(pixel_positions, intensity)
plt.xlabel('Pixel Position')
plt.ylabel('Intensity')
plt.title('Spectrum: Intensity vs. Pixel Position')
plt.show()
# Main function to capture, process and plot the spectrum
def main():
image = capture_image()
if image is not None:
intensity = process_image(image)
plot_spectrum(intensity)
if __name__ == "__main__":
main()