if name == 'main':
# Read input video filename
filename = r'focus-test.mp4'
code
# Create a VideoCapture object
cap = cv2.imread(filename)
# Read first frame from the video
ret, frame = cap.read()
# Display total number of frames in the video
print("Total number of frames : {}".format(int(cap.get(cv2.CAP_PROP_FRAME_COUNT))))
# Max measure of focus
maxV = 0
# Frame with maximum measure of focus
bestFrame = 0
# Frame ID of frame with maximum measure of focus
bestFrameId = 0
# Get measures of focus from both methods
val = var_abs_laplacian(frame)
# Specify the ROI for the flower in the frame
top = 50
bottom = 150
left = 200
right = 300
# Iterate over all the frames present in the video
while(ret):
# Crop the flower region out of the frame
flower = frame[top:bottom, left:right]
# Get measures of focus
val = var_abs_laplacian(frame)
# If the current measure of focus is greater than the current maximum
if val > maxV:
# Revise the current maximum
maxV = val
# Get frame ID of the new best frame
bestFrameId = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
# Revise the new best frame
bestFrame = frame.copy()
# Read a new frame
ret, frame = cap.read()
print("================================================")
print("Frame ID of the best frame: {}".format(bestFrameId))
cap.release()
cv2.namedWindow("Best Frame", cv2.WINDOW_AUTOSIZE)
cv2.imshow("Best Frame", bestFrame)
cv2.waitKey()上述代码运行时出现错误Traceback (most recent call last):
File "D:\pycharm\opencv\test1.py", line 21, in
ret, frame = cap.read()
AttributeError: 'NoneType' object has no attribute 'read'怎么解决
if name == 'main':
# Read input video filename
filename = r'focus-test.mp4'
code
# Create a VideoCapture object
cap = cv2.VideoCapture(filename)
# Read first frame from the video
ret, frame = cap.read()
# Display total number of frames in the video
print("Total number of frames : {}".format(int(cap.get(cv2.CAP_PROP_FRAME_COUNT))))
# Max measure of focus
maxV = 0
# Frame with maximum measure of focus
bestFrame = 0
# Frame ID of frame with maximum measure of focus
bestFrameId = 0
# Get measures of focus from both methods
val = var_abs_laplacian(frame)
# Specify the ROI for the flower in the frame
top = 50
bottom = 150
left = 200
right = 300
# Iterate over all the frames present in the video
while(ret):
# Crop the flower region out of the frame
flower = frame[top:bottom, left:right]
# Get measures of focus
val = var_abs_laplacian(frame)
# If the current measure of focus is greater than the current maximum
if val > maxV:
# Revise the current maximum
maxV = val
# Get frame ID of the new best frame
bestFrameId = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
# Revise the new best frame
bestFrame = frame.copy()
# Read a new frame
ret, frame = cap.read()
print("================================================")
print("Frame ID of the best frame: {}".format(bestFrameId))
cap.release()
cv2.namedWindow("Best Frame", cv2.WINDOW_AUTOSIZE)
cv2.imshow("Best Frame", bestFrame)
cv2.waitKey()将这个代码中的laplacian的方法改为sobel算子方法
Assistant
要将代码中的Laplacian方法更改为Sobel算子方法,可以使用cv2.Sobel()函数。
以下是更新后的代码:
python
import cv2
def var_abs_sobel(image):
kernelSize = 3
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=kernelSize)
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=kernelSize)
sobel = cv2.magnitude(sobelx, sobely)
sobel_square = sobel * sobel
measure = sobel_square.sum()
return measure
if __name__ == '__main__':
# Read input video filename
filename = r'focus-test.mp4'
# Create a VideoCapture object
cap = cv2.VideoCapture(filename)
# Read first frame from the video
ret, frame = cap.read()
# Display total number of frames in the video
print("Total number of frames : {}".format(int(cap.get(cv2.CAP_PROP_FRAME_COUNT))))
# Max measure of focus
maxV = 0
# Frame with maximum measure of focus
bestFrame = 0
# Frame ID of frame with maximum measure of focus
bestFrameId = 0
# Get measures of focus from both methods
val = var_abs_sobel(frame)
# Specify the ROI for the flower in the frame
top = 50
bottom = 150
left = 200
right = 300
# Iterate over all the frames present in the video
while(ret):
# Crop the flower region out of the frame
flower = frame[top:bottom, left:right]
# Get measures of focus
val = var_abs_sobel(frame)
# If the current measure of focus is greater than the current maximum
if val > maxV:
# Revise the current maximum
maxV = val
# Get frame ID of the new best frame
bestFrameId = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
# Revise the new best frame
bestFrame = frame.copy()
# Read a new frame
ret, frame = cap.read()
print("================================================")
print("Frame ID of the best frame: {}".format(bestFrameId))
cap.release()
cv2.namedWindow("Best Frame", cv2.WINDOW_AUTOSIZE)
cv2.imshow("Best Frame", bestFrame)
cv2.waitKey()
import cv2
def var_abs_sobel(frame):
kernelSize = 3
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=kernelSize)
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=kernelSize)
sobel = cv2.magnitude(sobelx, sobely)
sobel_square = sobel * sobel
measure = sobel_square.sum()
return measure
if __name__ == '__main__':
# Read input video filename
filename = r'focus-test.mp4'
# Create a VideoCapture object
cap = cv2.VideoCapture(filename)
# Read first frame from the video
ret, frame = cap.read()
# Display total number of frames in the video
print("Total number of frames: {}".format(int(cap.get(cv2.CAP_PROP_FRAME_COUNT))))
# Max measure of focus
maxV = 0
# Frame with maximum measure of focus
bestFrame = 0
# Frame ID of frame with maximum measure of focus
bestFrameId = 0
# Get measures of focus from both methods
val = var_abs_sobel(frame)
# Specify the ROI for the flower in the frame
top = 50
bottom = 150
left = 200
right = 300
# Iterate over all the frames present in the video
while ret:
# Crop the flower region out of the frame
flower = frame[top:bottom, left:right]
# Get measures of focus
val = var_abs_sobel(frame)
# If the current measure of focus is greater than the current maximum
if val > maxV:
# Revise the current maximum
maxV = val
# Get frame ID of the new best frame
bestFrameId = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
# Revise the new best frame
bestFrame = frame.copy()
# Read a new frame
ret, frame = cap.read()
print("================================================")
print("Frame ID of the best frame: {}".format(bestFrameId))
cap.release()
cv2.namedWindow("Best Frame", cv2.WINDOW_AUTOSIZE)
cv2.imshow("Best Frame", bestFrame)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
def var_abs_sobel(frame):
kernelSize = 3
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=kernelSize)
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=kernelSize)
sobel = cv2.magnitude(sobelx, sobely)
sobel_square = sobel * sobel
measure = sobel_square.sum()
# Normalize the sobel image to 8-bit range
sobel_normalized = cv2.normalize(sobel, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
cv2.imshow("Sobel", sobel_normalized)
cv2.waitKey(0)
cv2.destroyAllWindows()
return measure
if __name__ == '__main__':
# Read input video filename
filename = r'focus-test.mp4'
# Create a VideoCapture object
cap = cv2.VideoCapture(filename)
# Read first frame from the video
ret, frame = cap.read()
# Display total number of frames in the video
print("Total number of frames: {}".format(int(cap.get(cv2.CAP_PROP_FRAME_COUNT))))
# Max measure of focus
maxV = 0
# Frame with maximum measure of focus
bestFrame = None
# Frame ID of frame with maximum measure of focus
bestFrameId = 0
# Get measures of focus from both methods
val = var_abs_sobel(frame)
# Specify the ROI for the flower in the frame
top = 50
bottom = 150
left = 200
right = 300
# Iterate over all the frames present in the video
while ret:
# Crop the flower region out of the frame
flower = frame[top:bottom, left:right]
# Get measures of focus
val = var_abs_sobel(frame)
# If the current measure of focus is greater than the current maximum
if val > maxV:
# Revise the current maximum
maxV = val
# Get frame ID of the new best frame
bestFrameId = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
# Revise the new best frame
bestFrame = frame.copy()
# Read a new frame
ret, frame = cap.read()
print("================================================")
print("Frame ID of the best frame: {}".format(bestFrameId))
cap.release()
cv2.namedWindow("Best Frame", cv2.WINDOW_AUTOSIZE)
cv2.imshow("Best Frame", bestFrame)
cv2.waitKey(0)
cv2.destroyAllWindows()
User
问题出自于提供的name和main书写问题,需改成__name__和__main__
Assistant
非常抱歉,我犯了一个错误。你是对的,正确的语法是__name__而不是name和main。
以下是更新的代码:
python
import cv2
def var_abs_sobel(frame):
kernelSize = 3
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=kernelSize)
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=kernelSize)
sobel = cv2.magnitude(sobelx, sobely)
sobel_square = sobel * sobel
measure = sobel_square.sum()
# Normalize the sobel image to 8-bit range
sobel_normalized = cv2.normalize(sobel, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
cv2.imshow("Sobel", sobel_normalized)
cv2.waitKey(0)
cv2.destroyAllWindows()
return measure
if __name__ == '__main__':
# Read input video filename
filename = r'focus-test.mp4'
# Create a VideoCapture object
cap = cv2.VideoCapture(filename)
# Read first frame from the video
ret, frame = cap.read()
# Display total number of frames in the video
print("Total number of frames: {}".format(int(cap.get(cv2.CAP_PROP_FRAME_COUNT))))
# Max measure of focus
maxV = 0
# Frame with maximum measure of focus
bestFrame = None
# Frame ID of frame with maximum measure of focus
bestFrameId = 0
# Get measures of focus from both methods
val = var_abs_sobel(frame)
# Specify the ROI for the flower in the frame
top = 50
bottom = 150
left = 200
right = 300
# Iterate over all the frames present in the video
while ret:
# Crop the flower region out of the frame
flower = frame[top:bottom, left:right]