Skip to content

Commit f0be310

Browse files
Add files via upload
1 parent 21e21ed commit f0be310

12 files changed

+454
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import cv2
2+
3+
# Load an image from file
4+
image = cv2.imread('C:/Users/ASUS/bicycle-001.jpg')
5+
6+
# Display the image in a window
7+
cv2.imshow('Image', image)
8+
9+
# Wait for a key press and close the window
10+
cv2.waitKey(0)
11+
cv2.destroyAllWindows()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import cv2
2+
3+
# Read image in grayscale
4+
img_grayscale = cv2.imread('C:/Users/ASUS/bruce-lee-1972.jpg', 0)
5+
6+
# Display the image in a window
7+
cv2.imshow('grayscale image', img_grayscale)
8+
cv2.waitKey(0)
9+
10+
# Save the image to a file
11+
cv2.imwrite('C:/Users/ASUS/OpenCV-Demos/bruce-lee-1972_grayscale.jpg', img_grayscale)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import cv2
2+
3+
# Load an image from file
4+
image = cv2.imread('C:/Users/ASUS/bruce-lee-1972.jpg')
5+
6+
# Display the image in a window
7+
cv2.imshow('Image', image)
8+
9+
# Wait for a key press and close the window
10+
cv2.waitKey(0)
11+
cv2.destroyAllWindows()
12+
13+
# Save the image to a file
14+
cv2.imwrite('C:/Users/ASUS/OpenCV-Demos/bruce-lee-1972.jpg', image)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import cv2 as cv
2+
3+
cap = cv.VideoCapture(0) # Use 0 for the default camera
4+
if not cap.isOpened():
5+
print("Cannot open camera")
6+
exit()
7+
8+
while True:
9+
ret, frame = cap.read()
10+
if not ret:
11+
print("Can't receive frame (stream end?). Exiting ...")
12+
break
13+
14+
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
15+
cv.imshow('frame', gray)
16+
17+
if cv.waitKey(1) == ord('q'):
18+
break
19+
20+
cap.release()
21+
cv.destroyAllWindows()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import cv2
2+
3+
# Capture video from file
4+
cap = cv2.VideoCapture('C:/Users/ASUS/OpenCV+ChatGPT/5158727-sd_640_360_30fps.mp4')
5+
6+
# Check if the video opened successfully
7+
if not cap.isOpened():
8+
print("Error: Could not open video.")
9+
else:
10+
# Read until the video is completed
11+
while cap.isOpened():
12+
# Capture frame-by-frame
13+
ret, frame = cap.read()
14+
15+
if ret:
16+
# Display the resulting frame
17+
cv2.imshow('Frame', frame)
18+
19+
# Press Q on keyboard to exit
20+
if cv2.waitKey(25) & 0xFF == ord('q'):
21+
break
22+
else:
23+
break
24+
25+
# When everything done, release the video capture object
26+
cap.release()
27+
28+
# Close all OpenCV windows
29+
cv2.destroyAllWindows()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import cv2
2+
3+
# Capture video from file
4+
cap = cv2.VideoCapture('C:/Users/ASUS/OpenCV+ChatGPT/5158727-sd_640_360_30fps.mp4')
5+
6+
# Define the codec and create VideoWriter object
7+
fourcc = cv2.VideoWriter_fourcc(*'XVID') # Specify the codec
8+
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
9+
10+
# Check if the video opened successfully
11+
if not cap.isOpened():
12+
print("Error: Could not open video.")
13+
else:
14+
# Read until the video is completed
15+
while cap.isOpened():
16+
# Capture frame-by-frame
17+
ret, frame = cap.read()
18+
19+
if ret:
20+
# Resize the frame if necessary
21+
frame = cv2.resize(frame, (640, 480))
22+
23+
# Write the frame into the output file
24+
out.write(frame)
25+
26+
# Display the resulting frame
27+
cv2.imshow('Frame', frame)
28+
29+
# Press Q on keyboard to exit
30+
if cv2.waitKey(25) & 0xFF == ord('q'):
31+
break
32+
else:
33+
break
34+
35+
# When everything done, release the video capture and writer objects
36+
cap.release()
37+
out.release()
38+
39+
# Close all OpenCV windows
40+
cv2.destroyAllWindows()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from delphivcl import *
2+
import cv2
3+
import numpy as np
4+
5+
class MainForm(Form):
6+
7+
def __init__(self, owner):
8+
self.Caption = "OpenCV with DelphiVCL4Python"
9+
self.SetBounds(100, 100, 800, 600)
10+
11+
self.Image = Image(self)
12+
self.Image.Parent = self
13+
self.Image.SetBounds(10, 10, 640, 480)
14+
15+
self.Button = Button(self)
16+
self.Button.Parent = self
17+
self.Button.SetBounds(660, 10, 120, 40)
18+
self.Button.Caption = "Capture Image"
19+
self.Button.OnClick = self.ButtonOnClick
20+
21+
def ButtonOnClick(self, Sender):
22+
# Capture video from camera
23+
cap = cv2.VideoCapture(0) # Use 0 for the default camera
24+
25+
if not cap.isOpened():
26+
print("Error: Could not open video.")
27+
return
28+
29+
# Read one frame
30+
ret, frame = cap.read()
31+
cap.release()
32+
33+
if not ret:
34+
print("Error: Could not read frame.")
35+
return
36+
37+
# Convert the frame to BMP format
38+
_, buffer = cv2.imencode(".bmp", frame)
39+
bmp_data = buffer.tobytes()
40+
41+
# Load the image into the TImage component
42+
stream = MemoryStream(len(bmp_data))
43+
stream.Write(bmp_data)
44+
stream.Position = 0
45+
self.Image.Picture.Bitmap.LoadFromStream(stream)
46+
stream.Free()
47+
48+
def main():
49+
Application.Initialize()
50+
Application.Title = "DelphiVCL4Python with OpenCV"
51+
main_form = MainForm(Application)
52+
Application.Run()
53+
54+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import tkinter as tk
2+
import cv2
3+
from PIL import Image, ImageTk
4+
5+
# Create a Tkinter window
6+
window = tk.Tk()
7+
window.title("OpenCV with Tkinter")
8+
9+
# Load an image using OpenCV
10+
cv_img = cv2.cvtColor(cv2.imread("C:/Users/ASUS/OpenCV+ChatGPT/komodo-001.jpg"), cv2.COLOR_BGR2RGB)
11+
12+
# Get the image dimensions
13+
height, width, _ = cv_img.shape
14+
15+
# Create a canvas to display the image
16+
canvas = tk.Canvas(window, width=width, height=height)
17+
canvas.pack()
18+
19+
# Convert the NumPy ndarray to a PhotoImage
20+
photo = ImageTk.PhotoImage(image=Image.fromarray(cv_img))
21+
22+
# Add the PhotoImage to the Canvas
23+
canvas.create_image(0, 0, image=photo, anchor=tk.NW)
24+
25+
# Run the Tkinter event loop
26+
window.mainloop()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Import the necessary packages
2+
from tkinter import *
3+
from PIL import Image
4+
from PIL import ImageTk
5+
import tkinter.filedialog
6+
import cv2
7+
8+
def select_image():
9+
# Grab a reference to the image panels
10+
global panelA, panelB
11+
# Open a file chooser dialog and allow the user to select an input image
12+
path = tkinter.filedialog.askopenfilename()
13+
14+
# Ensure a file path was selected
15+
if len(path) > 0:
16+
# Load the image from disk, convert it to grayscale, and detect edges in it
17+
image = cv2.imread(path)
18+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
19+
edged = cv2.Canny(gray, 50, 100)
20+
# OpenCV represents images in BGR order; however PIL represents images in RGB order, so we need to swap the channels
21+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
22+
# Convert the images to PIL format
23+
image = Image.fromarray(image)
24+
edged = Image.fromarray(edged)
25+
# and then to ImageTk format
26+
image = ImageTk.PhotoImage(image)
27+
edged = ImageTk.PhotoImage(edged)
28+
29+
# If the panels are None, initialize them
30+
if panelA is None or panelB is None:
31+
# The first panel will store our original image
32+
panelA = Label(image=image)
33+
panelA.image = image
34+
panelA.pack(side="left", padx=10, pady=10)
35+
# While the second panel will store the edge map
36+
panelB = Label(image=edged)
37+
panelB.image = edged
38+
panelB.pack(side="right", padx=10, pady=10)
39+
# Otherwise, update the image panels
40+
else:
41+
# Update the panels
42+
panelA.configure(image=image)
43+
panelB.configure(image=edged)
44+
panelA.image = image
45+
panelB.image = edged
46+
47+
# Initialize the window toolkit along with the two image panels
48+
root = Tk()
49+
panelA = None
50+
panelB = None
51+
# Create a button, then when pressed, will trigger a file chooser dialog and allow the user to select an input image; then add the button to the GUI
52+
btn = Button(root, text="Select an image", command=select_image)
53+
btn.pack(side="bottom", fill="both", expand="yes", padx="10", pady="10")
54+
# Kick off the GUI
55+
root.mainloop()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import cv2
2+
from tkinter import *
3+
from tkinter import ttk
4+
from PIL import Image, ImageTk
5+
6+
class App:
7+
def __init__(self, root):
8+
self.root = root
9+
self.root.title("OpenCV with Tkinter")
10+
11+
self.label = ttk.Label(root)
12+
self.label.grid(row=0, column=0, padx=10, pady=10)
13+
14+
self.button = ttk.Button(root, text="Capture Image", command=self.capture_image)
15+
self.button.grid(row=1, column=0, padx=10, pady=10)
16+
17+
self.cap = cv2.VideoCapture(0) # Use 0 for the default camera
18+
19+
def capture_image(self):
20+
if not self.cap.isOpened():
21+
print("Error: Could not open video.")
22+
return
23+
24+
# Read one frame
25+
ret, frame = self.cap.read()
26+
27+
if not ret:
28+
print("Error: Could not read frame.")
29+
return
30+
31+
# Convert the frame to RGB format
32+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
33+
34+
# Convert the frame to a PIL image
35+
img = Image.fromarray(frame)
36+
37+
# Convert the PIL image to a PhotoImage
38+
imgtk = ImageTk.PhotoImage(image=img)
39+
40+
# Display the image in the label
41+
self.label.imgtk = imgtk
42+
self.label.configure(image=imgtk)
43+
44+
def __del__(self):
45+
if self.cap.isOpened():
46+
self.cap.release()
47+
48+
if __name__ == "__main__":
49+
root = Tk()
50+
app = App(root)
51+
root.mainloop()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import sys
2+
import cv2
3+
from PyQt5.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget
4+
from PyQt5.QtGui import QImage, QPixmap
5+
from PyQt5.QtCore import Qt
6+
from PIL import Image
7+
8+
class ImageViewer(QWidget):
9+
def __init__(self):
10+
super().__init__()
11+
self.setWindowTitle("OpenCV with PyQt")
12+
self.setGeometry(100, 100, 800, 600)
13+
14+
# Load an image using OpenCV
15+
cv_img = cv2.cvtColor(cv2.imread('got.jpg'), cv2.COLOR_BGR2RGB)
16+
17+
# Convert the image to QImage
18+
height, width, channel = cv_img.shape
19+
bytes_per_line = 3 * width
20+
q_img = QImage(cv_img.data, width, height, bytes_per_line, QImage.Format_RGB888)
21+
22+
# Create a QLabel to display the image
23+
self.label = QLabel(self)
24+
self.label.setPixmap(QPixmap.fromImage(q_img))
25+
26+
# Set layout
27+
layout = QVBoxLayout()
28+
layout.addWidget(self.label)
29+
self.setLayout(layout)
30+
31+
if __name__ == "__main__":
32+
app = QApplication(sys.argv)
33+
viewer = ImageViewer()
34+
viewer.show()
35+
sys.exit(app.exec_())

0 commit comments

Comments
 (0)