Introduction to image analysis opencv python

Install the following on your mac

brew update
brew install -v cmake
brew tap homebrew/science
brew install opencv
export PYTHONPATH="/usr/local/lib/python2.7/site-packages/:$PYTHONPATH"

Capture and display images

Take an image of Messi and save it as "messi.jpeg " messi5.jpeg

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import cv2

 
#Load the original image
img = cv2.imread('messi.jpeg',cv2.IMREAD_UNCHANGED)
#Show in window
cv2.imshow("result", img)
#Export
cv2.imwrite('img.jpg', img)
#End processing
cv2.waitKey(0)
cv2.destroyAllWindows()

If it doesn't finish, press control + z to kill it

Make it grayscale

gray_img = cv2.imread('messi.jpeg', cv2.IMREAD_GRAYSCALE)
cv2.imshow("result", gray_img)

gray_messi.png

Only contour

canny_img = cv2.Canny(gray_img, 50, 110)
cv2.imshow("result", canny_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

canny_messi.png


#Change the strength of the contour
cv2.Canny()Is a function that processes by the Canny method and sets two thresholds. The higher the number, the less contours will be written.
Click here for details http://postd.cc/image-processing-101/

canny_img = cv2.Canny(gray_img, 200, 400)

![canny_img.jpg](https://qiita-image-store.s3.amazonaws.com/0/124303/8c6aa42f-891c-b165-04b3-75b83c929e12.jpeg)

#Detect the ball
If the object of interest is a standard form such as a line or circle, it can be detected using the Hough transform.

circles = cv2.HoughCircles(canny_img, cv2.cv.CV_HOUGH_GRADIENT, dp=2, minDist=50, param1=20, param2=30, minRadius=5, maxRadius=20 ) print circles import matplotlib.pyplot as plt plt.imshow(canny_img) plt.show()

Parameters

dp ・ ・ ・ Increase if the resolution of the original image is reduced and detected during processing. For example, if it is 1, it will be processed with the same image quality, and if it is 2, it will be reduced to 1/2. minDist ・ ・ ・ Minimum distance between circles detected param1 ・ ・ ・ It seems that "the higher of the two thresholds used in Canny's edge detector". The lower it is, the more edges are detected. param2 ・ ・ ・ Threshold value at the time of center detection calculation. The lower it is, the more non-circular it is detected. minRadius ・ ・ ・ Minimum radius maxRadius ・ ・ ・ Maximum radius

Then, 5 candidates came out[x,y,radius]

[[[ 125. 51. 16.27882004] [ 193. 161. 5.83095169] [ 131. 155. 8.24621105] [ 71. 73. 18.02775574] [ 161. 91. 17.80449295]]]


Plot the circle on the original image

cups_circles = np.copy(img)

if circles is not None and len(circles) > 0: circles = circles[0] for (x, y, r) in circles: x, y, r = int(x), int(y), int(r) cv2.circle(cups_circles, (x, y), r, (255, 255, 0), 4) plt.imshow(cv2.cvtColor(cups_circles, cv2.COLOR_BGR2RGB)) plt.show()

print('number of circles detected: %d' % len(circles[0]))

![figure_1.png](https://qiita-image-store.s3.amazonaws.com/0/124303/be507713-acfe-917d-fdcc-2902950aa7e6.png)

Hmm ... it's correct, but something strange is also counted

By adjusting the parameters etc., it was possible to make only the face and the ball
(I felt like I should stop grayscale)
![figure_1.png](https://qiita-image-store.s3.amazonaws.com/0/124303/70aa9f57-4307-8af4-87d4-bac6e64b580b.png)

!/usr/bin/env python -- coding: utf-8 -- import numpy as np import cv2 import matplotlib.pyplot as plt

img = cv2.imread('messi5.jpeg',cv2.IMREAD_UNCHANGED) canny_img = cv2.Canny(img, 230, 500)

find hough circles circles = cv2.HoughCircles(canny_img, cv2.cv.CV_HOUGH_GRADIENT, dp=2, minDist=100, param1=25, param2=30, minRadius=5, maxRadius=20 ) print circles

cups_circles = np.copy(img)

if circles are detected, draw them if circles is not None and len(circles) > 0: # note: cv2.HoughCircles returns circles nested in an array. # the OpenCV documentation does not explain this return value format circles = circles[0] for (x, y, r) in circles: x, y, r = int(x), int(y), int(r) cv2.circle(cups_circles, (x, y), r, (255, 255, 0), 4) plt.imshow(cv2.cvtColor(cups_circles, cv2.COLOR_BGR2RGB)) plt.show()

cv2.waitKey(0) cv2.destroyAllWindows()


How. It looks more beautiful when you apply the color fista
http://stackoverflow.com/questions/22870948/how-can-i-pythonically-us-opencv-to-find-a-a-basketball-in-an-image
http://www.pyimagesearch.com/2015/09/14/ball-tracking-with-opencv/


If you make it into an application, it will be helpful
http://www.melt.kyutech.ac.jp/2015/onoue.pdf#search='opencv+ball+%E7%B2%BE%E5%BA%A6'

How can you make a chasing camera with Raspberry Pi!
https://www.youtube.com/watch?v=58xxn6d_bUg

Next, let's try a video tutorial
http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_gui/py_video_display/py_video_display.html


Recommended Posts

Introduction to image analysis opencv python
Introduction to OpenCV (python)-(2)
Introduction to Python language
How to crop an image with Python + OpenCV
opencv-python Introduction to image processing
Introduction to Python Django (2) Win
Image editing with python OpenCV
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
Introduction to Python For, While
[OpenCV / Python] I tried image analysis of cells with OpenCV
Reading Note: An Introduction to Data Analysis with Python
[Technical book] Introduction to data analysis using Python -1 Chapter Introduction-
Python OpenCV tried to display the image in text.
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments
[Python] Using OpenCV with Python (Image Filtering)
Introduction to Python Numerical Library NumPy
Practice! !! Introduction to Python (Type Hints)
[Python] Using OpenCV with Python (Image transformation)
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
[Introduction to Udemy Python 3 + Application] 57. Decorator
Introduction to Python Hands On Part 1
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Python] How to parse JSON
Workflow to convert formula (image) to python
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
Introduction to Protobuf-c (C language ⇔ Python)
20200329_Introduction to Data Analysis with Python Second Edition Personal Summary
[Introduction to Udemy Python3 + Application] 59. Generator
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
[Introduction to Udemy Python 3 + Application] Summary
[Introduction to Python] Let's use pandas
An introduction to Python for non-engineers
Introduction to Python Django (2) Mac Edition
[AWS SAM] Introduction to Python version
[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)
[Python Tutorial] An Easy Introduction to Python
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
Introduction to Python Basics of Machine Learning (Unsupervised Learning / Principal Component Analysis)
Introduction to Structural Equation Modeling (SEM), Covariance Structure Analysis with Python
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
I tried to make an image similarity function with Python + OpenCV
[Introduction to Udemy Python3 + Application] 18. List methods
[Introduction to Udemy Python3 + Application] 28. Collective type
[Introduction to Python] How to use class in Python?
HTML email with image to send with python
[Introduction to Udemy Python3 + Application] 25. Dictionary-type method
[Introduction to Udemy Python3 + Application] 33. if statement
Introduction to Discrete Event Simulation Using Python # 1
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
[Introduction to Udemy Python3 + Application] 55. In-function functions
[Introduction to Udemy Python3 + Application] 48. Function definition
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
A super introduction to Python bit operations
[Introduction to Udemy Python 3 + Application] 10. Numerical values
An introduction to OpenCV for machine learning
[Introduction to Udemy Python3 + Application] 21. Tuple type