[PYTHON] About Opencv ②

Notes about Opencv3 series

Basically, it is a thing while checking the official document.

Previous memo About Opencv ①

The environment and images used are the same as last time.

① Take a video

  1. cv2.VIdeoCapture (first argument)

opencv.ipynb


cap = cv2.VideoCapture(0)

Set 0 or 1 in the first argument. If you use a USB camera, you can use it by entering numbers.

Please note that the order in which numbers are assigned to USB cameras changes when the power is turned on.

Things with a camera are usually recognized first on a laptop, so you can recognize them with 0.

The retrofit USB camera can be recognized by setting 1.

However, as a phenomenon that existed in the past, it is NG if you use opencv to display a camera with a USB3.0 connection made overseas. It is possible that the camera could not be used because it was a camera controlled via software dedicated to the manufacturer.

I think it's okay because you can usually go to commercial products.

Refer to the official notation

opencv.ipynb


device_id = 0
cap = cv2.VideoCapture(device_id)

cap.set(3,640)
cap.set(4,480)
cap.set(5,50)

while(True):
    ret,frame = cap.read()
  gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
  cv2.imshow("frame",gray)

cap.release()
cv2.destroyAllWindows()

For cap.set () See Official Documentation.

To be exact, it is cap.set (first argument, second argument) First argument: propId 2nd argument: value etc.

スクリーンショット (340).png

From the top of the list are 0, 1, 2 ... As a frequently used item 3: CV_CAP_PROP_FRAME_WIDTH Specify the width of the display image 4: CV_CAP_PROP_FRAME_HEIGHT Specify the vertical width of the display image 5: CV_CAP_PROP_FPS Specify the FPS value of the displayed image

cap.set (3,640) will have a width of 640.

Even if you don't use it, it will be displayed with the camera default settings, so I think there is no problem.


cap.read () returns two values, True/False. what the hell?

opencv.ipynb


#When there is image data
print(ret)
>>>True

#When there is no image data
print(ret)
>>>False

Image data is like being stored in a frame.

cv2.cvtColor (src, code [, dst [, dstCn]]) * Official notation The notation that seems to be refused at first glance ...

I haven't grasped the detailed usage yet I'm wondering if cv2.cvtColor (image, processing method) is fine.

If you go this time, cv2.cvtColor (frame, cv2.COLOR_BGR2GRAY) Recognition to grayscale the image data in the frame

cv2.COLOR_BGR2BGRA BGR array (color) cv2.COLOR_RGB2RGBA RGB array (color) cv2.COLOR_BGRA2BGR BGR array (color) cv2.COLOR_RGBA2RGB RGB array (color)

cv2.COLOR_BGR2GRAY BGR array (gray conversion) cv2.COLOR_RGB2GRAY RGB array (gray conversion)

The leftmost is the original image (the image data is converted, not the video) スクリーンショット (343).png The leftmost is the original image ((The image data is converted, not the video) スクリーンショット (344).png

There is no change from the image data, When I used the data acquired by VideoCapture, I saw a lot of changes. I think it's interesting to try it.

There are many other things, but for details, see See here.


② Save the video data.

opencv.ipynb


device_id = 0
cap = cv2.VideoCapture(device_id)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)

        # write the flipped frame
        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

Officially it is explained like this, but I wonder if it is necessary to make it so complicated I think this is the best if it is true I've tried a simple method before the official one, so I've never used it honestly.


opencv.ipynb


device_id = 0
cap = cv2.VideoCapture(device_id)

while True:
    ret,frame = cap.read()
    cv2.imshow(basename,frame)
    key = cv2.waitKey(1) & 0xFF    
    if key == ord('o'):
        cv2.imwrite("file name",frame)
 
    elif key == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

cv2.imwrite (1st argument, 2nd argument) You can use this to save. The first argument is the file name The second argument is frame

You can now save it without any problems.

You can also go by specifying the save destination for the first argument.

opencv.ipynb


base_path = "Destination"
base_name = "file name"

cv2.imwrite((base_path + datename + ".png "),frame)

So far, I haven't had any problems with the above method.


③ Summary

If you just want to take an image, you can combine this.

However, since it is a combination with image processing, I think it will be diverse.

The processing will be described separately.

In addition, we will correct the incorrect part each time.

Recommended Posts

About Opencv ②
About Opencv ③
About Opencv ①
Summary about Python3 + OpenCV3
About LangID
About CAGR
About virtiofs
About python-apt
About Permission
About sklearn.preprocessing.Imputer
About gunicorn
About requirements.txt
About locale
About permissions
About axis = 0, axis = 1
About import
About numpy
About pip
About Linux
About numpy.newaxis
About endian
About Linux
About import
About Linux
About Linux
About Linux ①
About cv2.imread
About _ and __
About wxPython
[OpenCV] About the array returned by imread
Adjust OpenCV contrast
Notepad about TecoGAN
About python slices
Briefly about __name__
[OpenCV] Personal memo
About python comprehension
About Docker Volume
[Linux] About export
About reference type
About Twitter scraping
About the test
I tried'Beauty'with OpenCV
Learn about programming
About Flask customization
Python2.7 + CentOS7 + OpenCV3
About variable scope. .. ..
About Python tqdm.
Notes about with
About python, class
About Linear Models
About Go functions
OpenCV3 installation battle
About pandas describe
About Kivy root
About Firestore timeout
About python inheritance
About python, range ()
About Confusion Matrix
[Linux] About PATH
About python decorators
Linux (about groups)