Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_opencv
提交
38a19732
S
skill_tree_opencv
项目概览
CSDN 技术社区
/
skill_tree_opencv
通知
44
Star
9
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
2
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_opencv
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
2
Issue
2
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
38a19732
编写于
12月 16, 2021
作者:
F
feilong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
添加一个简单的鸟的识别
上级
44e3d9bc
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
142 addition
and
3 deletion
+142
-3
data/1.OpenCV初阶/2.二值图像处理/5.轮廓/Contours.py
data/1.OpenCV初阶/2.二值图像处理/5.轮廓/Contours.py
+4
-3
data/1.OpenCV初阶/5.图像识别/2.鸟图识别/.gitkeep
data/1.OpenCV初阶/5.图像识别/2.鸟图识别/.gitkeep
+0
-0
data/1.OpenCV初阶/5.图像识别/2.鸟图识别/bird.jpeg
data/1.OpenCV初阶/5.图像识别/2.鸟图识别/bird.jpeg
+0
-0
data/1.OpenCV初阶/5.图像识别/2.鸟图识别/birds_detect.jpeg
data/1.OpenCV初阶/5.图像识别/2.鸟图识别/birds_detect.jpeg
+0
-0
data/1.OpenCV初阶/5.图像识别/2.鸟图识别/detect_bird.md
data/1.OpenCV初阶/5.图像识别/2.鸟图识别/detect_bird.md
+103
-0
data/1.OpenCV初阶/5.图像识别/2.鸟图识别/detect_bird.py
data/1.OpenCV初阶/5.图像识别/2.鸟图识别/detect_bird.py
+35
-0
未找到文件。
data/1.OpenCV初阶/2.二值图像处理/5.轮廓/Contours.py
浏览文件 @
38a19732
...
...
@@ -5,8 +5,10 @@ if __name__ == '__main__':
imgGray
=
cv
.
cvtColor
(
img
,
cv
.
COLOR_BGR2GRAY
)
ret
,
thresh
=
cv
.
threshold
(
imgGray
,
127
,
255
,
cv
.
THRESH_BINARY_INV
)
image
,
contours
,
hierarchy
=
cv
.
findContours
(
thresh
,
cv
.
RETR_TREE
,
cv
.
CHAIN_APPROX_SIMPLE
)
contours
,
hierarchy
=
cv
.
findContours
(
thresh
,
cv
.
RETR_TREE
,
cv
.
CHAIN_APPROX_SIMPLE
)
contourPic
=
cv
.
drawContours
(
img
,
contours
,
-
1
,
(
0
,
0
,
255
),
2
)
cv
.
imshow
(
"ContourPicture"
,
contourPic
)
cv
.
waitKey
(
0
)
\ No newline at end of file
cv
.
waitKey
(
0
)
cv
.
destroyAllWindows
()
data/1.OpenCV初阶/5.图像识别/2.
车辆
识别/.gitkeep
→
data/1.OpenCV初阶/5.图像识别/2.
鸟图
识别/.gitkeep
浏览文件 @
38a19732
文件已移动
data/1.OpenCV初阶/5.图像识别/2.鸟图识别/bird.jpeg
0 → 100644
浏览文件 @
38a19732
328.9 KB
data/1.OpenCV初阶/5.图像识别/2.鸟图识别/birds_detect.jpeg
0 → 100644
浏览文件 @
38a19732
631.7 KB
data/1.OpenCV初阶/5.图像识别/2.鸟图识别/detect_bird.md
0 → 100644
浏览文件 @
38a19732
# 识别野外拍摄的鸟
使用基本的OpenCV轮廓检测识别出野外拍摄照片里的鸟
![](
./birds_detect.jpeg
)
基本框架如下:
```
python
import
cv2
if
__name__
==
'__main__'
:
img
=
cv2
.
imread
(
'bird.jpeg'
,
-
1
)
img_gray
=
cv2
.
cvtColor
(
img
,
cv2
.
COLOR_BGR2GRAY
)
ret
,
img_binary
=
cv2
.
threshold
(
img_gray
,
127
,
255
,
cv2
.
THRESH_BINARY
)
ret
,
bin_img
=
cv2
.
threshold
(
img_binary
,
0
,
255
,
cv2
.
THRESH_BINARY
+
cv2
.
THRESH_OTSU
)
# findCountours 函数在版本4之后返回参数只有2个
if
(
int
(
cv2
.
__version__
[
0
])
>
3
):
contours
,
hierarchy
=
cv2
.
findContours
(
bin_img
,
cv2
.
RETR_TREE
,
cv2
.
CHAIN_APPROX_SIMPLE
)
else
:
img2
,
contours
,
hierarchy
=
cv2
.
findContours
(
bin_img
,
cv2
.
RETR_TREE
,
cv2
.
CHAIN_APPROX_SIMPLE
)
# TODO(You): 请在此实现代码
cv2
.
imwrite
(
'birds_detect.jpeg'
,
img
)
cv2
.
imshow
(
"birds"
,
img
)
cv2
.
waitKey
(
0
)
cv2
.
destroyAllWindows
()
```
以下实现正确的代码是?
## 答案
```
python
sorted_contours
=
sorted
(
contours
,
key
=
cv2
.
contourArea
,
reverse
=
False
)
max_contour
=
sorted_contours
[
-
2
]
max_contour_area
=
cv2
.
contourArea
(
max_contour
)
bird_contours
=
filter
(
lambda
c
:
abs
(
cv2
.
contourArea
(
c
)
-
max_contour_area
)
<
1000
,
sorted_contours
)
for
c
in
bird_contours
:
x
,
y
,
w
,
h
=
cv2
.
boundingRect
(
c
)
cv2
.
rectangle
(
img
,
(
x
-
10
,
y
-
100
),
(
x
+
w
+
10
,
y
+
h
+
10
),
(
0
,
255
,
0
),
2
)
cv2
.
drawContours
(
img
,
c
,
-
1
,
(
0
,
0
,
255
),
3
)
```
## 选项
### 没有根据面积排序
```
python
sorted_contours
=
sorted
(
contours
,
reverse
=
False
)
max_contour
=
sorted_contours
[
-
2
]
max_contour_area
=
cv2
.
contourArea
(
max_contour
)
bird_contours
=
filter
(
lambda
c
:
abs
(
cv2
.
contourArea
(
c
)
-
max_contour_area
)
<
1000
,
sorted_contours
)
for
c
in
bird_contours
:
x
,
y
,
w
,
h
=
cv2
.
boundingRect
(
c
)
cv2
.
rectangle
(
img
,
(
x
-
10
,
y
-
100
),
(
x
+
w
+
10
,
y
+
h
+
10
),
(
0
,
255
,
0
),
2
)
cv2
.
drawContours
(
img
,
c
,
-
1
,
(
0
,
0
,
255
),
3
)
```
### 没有过滤出面积与最大鸟差不多的矩形
```
python
sorted_contours
=
sorted
(
contours
,
key
=
cv2
.
contourArea
,
reverse
=
False
)
max_contour
=
sorted_contours
[
-
2
]
max_contour_area
=
cv2
.
contourArea
(
max_contour
)
for
c
in
sorted_contours
:
x
,
y
,
w
,
h
=
cv2
.
boundingRect
(
c
)
cv2
.
rectangle
(
img
,
(
x
-
10
,
y
-
100
),
(
x
+
w
+
10
,
y
+
h
+
10
),
(
0
,
255
,
0
),
2
)
cv2
.
drawContours
(
img
,
c
,
-
1
,
(
0
,
0
,
255
),
3
)
```
### 过滤不对
```
python
sorted_contours
=
sorted
(
contours
,
key
=
cv2
.
contourArea
,
reverse
=
False
)
max_contour
=
sorted_contours
[
-
2
]
max_contour_area
=
cv2
.
contourArea
(
max_contour
)
bird_contours
=
filter
(
lambda
c
:
cv2
.
contourArea
(
c
)
>
max_contour_area
,
sorted_contours
)
for
c
in
bird_contours
:
x
,
y
,
w
,
h
=
cv2
.
boundingRect
(
c
)
cv2
.
rectangle
(
img
,
(
x
-
10
,
y
-
100
),
(
x
+
w
+
10
,
y
+
h
+
10
),
(
0
,
255
,
0
),
2
)
cv2
.
drawContours
(
img
,
c
,
-
1
,
(
0
,
0
,
255
),
3
)
```
data/1.OpenCV初阶/5.图像识别/2.鸟图识别/detect_bird.py
0 → 100644
浏览文件 @
38a19732
import
cv2
if
__name__
==
'__main__'
:
img
=
cv2
.
imread
(
'bird.jpeg'
,
-
1
)
img_gray
=
cv2
.
cvtColor
(
img
,
cv2
.
COLOR_BGR2GRAY
)
ret
,
img_binary
=
cv2
.
threshold
(
img_gray
,
127
,
255
,
cv2
.
THRESH_BINARY
)
ret
,
bin_img
=
cv2
.
threshold
(
img_binary
,
0
,
255
,
cv2
.
THRESH_BINARY
+
cv2
.
THRESH_OTSU
)
if
(
int
(
cv2
.
__version__
[
0
])
>
3
):
contours
,
hierarchy
=
cv2
.
findContours
(
bin_img
,
cv2
.
RETR_TREE
,
cv2
.
CHAIN_APPROX_SIMPLE
)
else
:
img2
,
contours
,
hierarchy
=
cv2
.
findContours
(
bin_img
,
cv2
.
RETR_TREE
,
cv2
.
CHAIN_APPROX_SIMPLE
)
sorted_contours
=
sorted
(
contours
,
key
=
cv2
.
contourArea
,
reverse
=
False
)
max_contour
=
sorted_contours
[
-
2
]
max_contour_area
=
cv2
.
contourArea
(
max_contour
)
print
(
max_contour_area
)
bird_contours
=
filter
(
lambda
c
:
abs
(
cv2
.
contourArea
(
c
)
-
max_contour_area
)
<
1000
,
sorted_contours
)
for
c
in
bird_contours
:
x
,
y
,
w
,
h
=
cv2
.
boundingRect
(
c
)
cv2
.
rectangle
(
img
,
(
x
-
10
,
y
-
100
),
(
x
+
w
+
10
,
y
+
h
+
10
),
(
0
,
255
,
0
),
2
)
cv2
.
drawContours
(
img
,
c
,
-
1
,
(
0
,
0
,
255
),
3
)
cv2
.
imwrite
(
'birds_detect.jpeg'
,
img
)
cv2
.
imshow
(
"birds"
,
img
)
cv2
.
waitKey
(
0
)
cv2
.
destroyAllWindows
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录