添加链接 注册    登录
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
暴躁的石榴  ·  OpenCV ...·  2 月前    · 
瘦瘦的棒棒糖  ·  OpenCV 4基础篇| ...·  2 月前    · 
会搭讪的骆驼  ·  Articles under the ...·  2 月前    · 
细心的豆腐  ·  Client Challenge·  5 月前    · 
听话的眼镜  ·  区块链泡沫中掘金: 巨头加码、 ...·  8 月前    · 
阳刚的红茶  ·  空调外机向邻家吹热风被起诉 ...·  1 年前    · 
帅气的小摩托  ·  华凯易佰去年净利润增长超五成 ...·  1 年前    · 
讲道义的闹钟  ·  extract() - Azure ...·  1 年前    · 
link管理  ›  Python for Image Recognition - OpenCV
python imread opencv
https://www.topcoder.com/thrive/articles/python-for-image-recognition-opencv
打盹的灭火器
2 年前

December 11, 2020

Python for Image Recognition - OpenCV

Thrive banner shape
article author avatar
Shubham Prasad whoami.kdm

DURATION

15min

categories

Development
How-To
Tools

Tags

Python
OpenCV
NumPy
Pandas

share

Share on LinkedIn Share on Facebook Share on Twitter

Looking to earn?

FREELANCE OPPORTUNITIES

OpenCV is an open-source image recognition library.
It is used for machine learning, computer vision and image processing. You can extract the most out of OpenCV when integrated with powerful libraries like Numpy and Pandas.

INSTALLATION PYTHON 3.X

Open Terminal/Command Prompt and type :
~ pip install opencv-python

GETTING STARTED (HOW TO READ IMAGES)

1.Open PyCharm.
2.Import cv2.
3.Paste a test image in the directory.
4.Create variable to store image using imread () function.
5. Display the image using imshow() function.
6. Add a delay using a waitkey () function.

1
import cv2
# LOAD AN IMAGE USING 'IMREAD'
img = cv2.imread("Resources/lena.png")
# DISPLAY
cv2.imshow("Lena Soderberg”, img)
    cv2.waitKey(0)

Screenshot 2020-12-11 10:19:07

PLAYING VIDEO USING VideoCapture() FUNCTION

  1. Open PyCharm.

  2. Import cv2.

  3. Paste a test video in the directory.

  4. Create variable to store video using VideoCapture() function.

  5. Create an infinite while loop to display each frame of the video continuously.

  6. Display the video using imshow() function.

  7. Add a delay using a waitkey() function.

1
import cv2
frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture("Resources/test_ video.mp4")
while True:
  success, img = cap.read()
img = cv2.resize(img, (frameWidth, frameHeight))
cv2.imshow("Result", img)
break

Screenshot 2020-12-11 10:20:17

ACCESSING LIVE FEED FROM WEBCAM

  1. Open PyCharm.

  2. Import cv2.

  3. Create variable to store video using VideoCapture() function.

  4. Pass parameter 0 in VideoCapture(0) to access webcam.

  5. Create an infinite while loop to display each frame of the webcam’s video continuously.

  6. Display the live feed using imshow() function.

  7. Add a delay of infinity using waitKey(0).

1
import cv2
Width = 640
Height = 480
cap = cv2.VideoCapture(0)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10, 150)
while True:
  success, img = cap.read()
cv2.imshow("Result", img)
break

FUNCTIONS OF OPENCV

Converting image to grayscale

  1. Open PyCharm.

  2. Import cv2.

  3. Create variable to store image using imread () function.

  4. To convert to grayscale use cv2 .cvtColor () function

  5. Pass the parameter image location and COLOR_BGR2GRAY to convert.

1
import cv2
img = cv2.imread("Resources/lena.png")
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("Gray Image", imgGray)
cv2.waitKey(0)

Screenshot 2020-12-11 10:22:26

Edge detection

  1. Open PyCharm.

  2. Import cv2.

  3. Create variable to store image using imread () function.

  4. To detect edge use cv2 .Canny () function

  5. Pass the parameter image location and threshold to convert.

1
import cv2
img = cv2.imread("Resources/lena.png")
imgCanny = cv2.Canny(img, 150, 200)
cv2.imshow("Canny Image”, imgCanny)
    cv2.waitKey(0)

Screenshot 2020-12-11 10:23:32

CROPPING IMAGE

  1. Import numpy and cv2.

  2. Create two variables to store the height and width of the image.

  3. Create two numpy arrays to store the coordinates.

  4. First array - store the coordinates of the image to be cropped.

  5. Second array - store the coordinates of the complete image.

  6. Crop the image using getPerspective() and wrapPerspective() function.

1
image = cv2.imread("Assets/cards.jpg")
width, height = 250, 350
point1 = np.float32([[111, 219], [287, 188], [154, 482], [352, 440]])
point2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]])
matrix = cv2.getPerspectiveTransform(point1, point2)
Output = cv2.warpPerspective(image, matrix, (width, height))
cv2.imshow("Image”, image)
    cv2.imshow("Output”, Output)
      cv2.waitKey(0)

Screenshot 2020-12-11 10:24:38

FACE DETECTION

  1. Open PyCharm.

  2. Import cv2.
    3.Create a variable to store cascade classifier (to learn more about cascade classifier click here.

  3. Convert image to greyscale using cv2 .cvtColor () function.

  4. Detect face using detectMultiscale () function.

  5. Draw a rectangle around the detected face.

1
import cv2
face_Cascade = cv2.CascadeClassifier("Resources/haarcascade_frontalface_default.xml")
image = cv2.imread('Resources/lena.png')
imgGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_Cascade.detectMultiScale(imgGray, 1.1, 4)
for (x, y, w, h) in faces:
  cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.imshow("Result", image)
cv2.waitKey(0)

Screenshot 2020-12-11 10:25:57

Click to show preference!
Click to show preference!
D0A3FC91-EEC2-4529-BF7D-3B777D79E185 Chat on Discord
 
推荐文章
暴躁的石榴  ·  OpenCV 图片的读取(imread和imdecode)、(并排)显示与保存(imwrite和imencode) - 一杯清酒邀明月
2 月前
瘦瘦的棒棒糖  ·  OpenCV 4基础篇| OpenCV图像基本操作开发者社区
2 月前
会搭讪的骆驼  ·  Articles under the label of opencv - AI备忘录
2 月前
细心的豆腐  ·  Client Challenge
5 月前
听话的眼镜  ·  区块链泡沫中掘金: 巨头加码、 连续创业者入场背后 - 21财经
8 月前
阳刚的红茶  ·  空调外机向邻家吹热风被起诉 法院:业主应拆除-中国法院网
1 年前
帅气的小摩托  ·  华凯易佰去年净利润增长超五成 AIGC应用大面积铺开
1 年前
讲道义的闹钟  ·  extract() - Azure Data Explorer & Real-Time Analytics | Microsoft Learn
1 年前
Link管理   ·   Sov5搜索   ·   小百科
link管理 - 链接快照平台