Opencv操作

关于opencv的安装包
https://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv

机器人相关视频

http://i.youku.com/u/UMTkxNDMxNDkxNg==

机器人历史及机器人应用_1
空间描述,广义坐标_2
[第3集] 东芝公司开发的柔性致动器
机器人“蜂鸟”_4
灵长类仿生机器人_5
[第6集] 瞬态运动学
[第7集] 雅可比矩阵显式
[第8集] 沙因曼机械臂
[第9集] 客座讲座:立体视觉
[第10集] 客座讲座:轨迹生成
关节空间动力学 11
[第12集] 拉格朗日方程
[第13集] 控制学综述
PD控制_14
机械臂控制 15
顺应性 16

这个视频是斯坦福大学08年的,关于一些原理,比较经典。

 

国内关于机器人相关的实体可编程网站,个人觉得比较好

https://www.yahboom.com/study/Omniduino

 

1、利用python进行识别相似图片(二)
https://segmentfault.com/a/1190000004500523
2、OpenCV-Python,计算机视觉开发利器 包括了:添加文字、图像处理、二值化,轮廓提取、灰度
模仿名画、人脸检测、姿态识别、风格迁移

# coding:utf-8
import cv2

# 加载模型
net = cv2.dnn.readNetFromTorch('the_scream.t7')
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
# 读取图片
image = cv2.imread('test.jpg')
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(image, 1.0, (w, h), (103.939, 116.779, 123.680), swapRB=False, crop=False)
# 进行计算
net.setInput(blob)
out = net.forward()
out = out.reshape(3, out.shape[2], out.shape[3])
out[0] += 103.939
out[1] += 116.779
out[2] += 123.68
out /= 255
out = out.transpose(1, 2, 0)
# 输出图片
cv2.imshow('Styled image', out)
cv2.waitKey(0)

 

 

# coding:utf-8
import cv2
import numpy


# pip install opencv-python
# pip install numpy
def oilPainting(img, templateSize, bucketSize, step):  # templateSize模板大小,bucketSize桶阵列,step模板滑动步长

    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    gray = ((gray / 256) * bucketSize).astype(int)  # 灰度图在桶中的所属分区
    h, w = img.shape[:2]

    oilImg = numpy.zeros(img.shape, numpy.uint8)  # 用来存放过滤图像

    for i in range(0, h, step):

        top = i - templateSize
        bottom = i + templateSize + 1
        if top < 0:
            top = 0
        if bottom >= h:
            bottom = h - 1

        for j in range(0, w, step):

            left = j - templateSize
            right = j + templateSize + 1
            if left < 0:
                left = 0
            if right >= w:
                right = w - 1

            # 灰度等级统计
            buckets = numpy.zeros(bucketSize, numpy.uint8)  # 桶阵列,统计在各个桶中的灰度个数
            bucketsMean = [0, 0, 0]  # 对像素最多的桶,求其桶中所有像素的三通道颜色均值
            # 对模板进行遍历
            for c in range(top, bottom):
                for r in range(left, right):
                    buckets[gray[c, r]] += 1  # 模板内的像素依次投入到相应的桶中,有点像灰度直方图

            maxBucket = numpy.max(buckets)  # 找出像素最多的桶以及它的索引
            maxBucketIndex = numpy.argmax(buckets)

            for c in range(top, bottom):
                for r in range(left, right):
                    if gray[c, r] == maxBucketIndex:
                        bucketsMean += img[c, r]
            bucketsMean = (bucketsMean / maxBucket).astype(int)  # 三通道颜色均值

            # 油画图
            for m in range(step):
                for n in range(step):
                    oilImg[m + i, n + j] = (bucketsMean[0], bucketsMean[1], bucketsMean[2])
    return oilImg

import os
os.chdir(r'C:\Users\Desktop\pic')
img = cv2.imread('20200108102403.jpg', cv2.IMREAD_ANYCOLOR)
oil = oilPainting(img, 4, 8, 2)
cv2.imshow('youhua', oil)
cv2.imwrite(r'myyouhua.jpg', oil)

 


https://segmentfault.com/a/1190000017026871

将图片进行素描的形式展示运行时间慢

# -*- coding: utf-8 -*-
from PIL import Image
from random import randint

old = Image.open(r"C:\Users\Desktop\pic\飞人.png")
new = Image.new('L', old.size, 255)
w, d = old.size
old = old.convert('L')
PEN_SIZE = 3
COLOR_DIFF = 7
LINE_LEN = 2

for i in range(PEN_SIZE + 1, w - PEN_SIZE - 1):
    for j in range(PEN_SIZE + 1, d - PEN_SIZE - 1):
        originalcolor = 255
        lcolor = sum([old.getpixel((i - r, j))
                      for r in range(PEN_SIZE)]) // PEN_SIZE
        rcolor = sum([old.getpixel((i + r, j))
                      for r in range(PEN_SIZE)]) // PEN_SIZE
        if abs(lcolor - rcolor) > COLOR_DIFF:
            originalcolor -= (255 - old.getpixel((i, j))) // 4
            for p in range(-LINE_LEN + randint(-1, 1), LINE_LEN + randint(-1, 1)):
                new.putpixel((i, j + p), originalcolor)

        ucolor = sum([old.getpixel((i, j - r))
                      for r in range(PEN_SIZE)]) // PEN_SIZE
        dcolor = sum([old.getpixel((i, j + r))
                      for r in range(PEN_SIZE)]) // PEN_SIZE
        if abs(ucolor - dcolor) > COLOR_DIFF:
            originalcolor -= (255 - old.getpixel((i, j))) // 4
            for p in range(-LINE_LEN + randint(-1, 1), LINE_LEN + randint(-1, 1)):
                new.putpixel((i + p, j), originalcolor)

        lucolor = sum([old.getpixel((i - r, j - r))
                       for r in range(PEN_SIZE)]) // PEN_SIZE
        rdcolor = sum([old.getpixel((i + r, j + r))
                       for r in range(PEN_SIZE)]) // PEN_SIZE
        if abs(lucolor - rdcolor) > COLOR_DIFF:
            originalcolor -= (255 - old.getpixel((i, j))) // 4
            for p in range(-LINE_LEN + randint(-1, 1), LINE_LEN + randint(-1, 1)):
                new.putpixel((i - p, j + p), originalcolor)

        rucolor = sum([old.getpixel((i + r, j - r))
                       for r in range(PEN_SIZE)]) // PEN_SIZE
        ldcolor = sum([old.getpixel((i - r, j + r))
                       for r in range(PEN_SIZE)]) // PEN_SIZE
        if abs(rucolor - ldcolor) > COLOR_DIFF:
            originalcolor -= (255 - old.getpixel((i, j))) // 4
            for p in range(-LINE_LEN + randint(-1, 1), LINE_LEN + randint(-1, 1)):
                new.putpixel((i + p, j + p), originalcolor)

new.save(r"pencil_drawing.jpg")

 

第二种办法

# -*- coding: utf-8 -*-
from PIL import Image
import numpy as np

a = np.asarray(Image.open(r'C:\Users\Desktop\pic\飞人.png').convert('L')).astype('float')
depth = 100.  # (0-100)
grad = np.gradient(a)  # 取图像灰度的梯度值
grad_x, grad_y = grad  # 分别取横纵图像梯度值
grad_x = grad_x * depth / 100.
grad_y = grad_y * depth / 100.
A = np.sqrt(grad_x ** 2 + grad_y ** 2 + 1.)
uni_x = grad_x / A
uni_y = grad_y / A
uni_z = 1. / A
vec_el = np.pi / 2.2  # 光源的俯视角度,弧度值
vec_az = np.pi / 4.  # 光源的方位角度,弧度值
dx = np.cos(vec_el) * np.cos(vec_az)  # 光源对x 轴的影响
dy = np.cos(vec_el) * np.sin(vec_az)  # 光源对y 轴的影响
dz = np.sin(vec_el)  # 光源对z 轴的影响
b = 255 * (dx * uni_x + dy * uni_y + dz * uni_z)  # 光源归一化
b = b.clip(0, 255)
im = Image.fromarray(b.astype('uint8'))  # 重构图像
im.save('new.jpg')

 


3、Python给照片换底色,基于opencv模块
https://segmentfault.com/a/1190000018599038?utm_source=sf-related

# coding:utf-8
import cv2
import numpy as np
import os


os.chdir(r'C:\Users\Desktop\pic')

img = cv2.imread('20200108102403.jpg')
# 缩放
rows, cols, channels = img.shape
img=cv2.resize(img, None, fx=0.5, fy=0.5)
rows, cols, channels = img.shape
cv2.imshow('img', img)
# 转换hsv
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_blue = np.array([90,70,70])
upper_blue = np.array([110,255,255])
mask = cv2.inRange(hsv, lower_blue, upper_blue)
cv2.imshow('Mask', mask)
# 腐蚀膨胀
erode = cv2.erode(mask, None, iterations=1)
cv2.imshow('erode', erode)
dilate = cv2.dilate(erode, None, iterations=1)
cv2.imshow('dilate', dilate)
# 遍历替换
for i in range(rows):
    for j in range(cols):
        if dilate[i, j] == 255:
            img[i, j] = (0, 0, 255)  # 此处替换颜色,为BGR通道

cv2.imshow('res', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

 


4、利用OpenCV提取图像中的矩形区域(PPT屏幕等)
https://segmentfault.com/a/1190000013925648?utm_source=sf-related
5、在Python中使用OpenCV进行人脸检测
https://segmentfault.com/a/1190000014942043
6、opencv python 图像修复
https://segmentfault.com/a/1190000015846719?utm_source=sf-related
7、超不清视频播放器-用Python将视频转成字符
https://segmentfault.com/a/1190000017749424

# coding: utf8
import cv2 as cv
import os
import time
# 替换字符列表
ascii_char = list(r"$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")
char_len = len(ascii_char)
# 加载视频
cap = cv.VideoCapture('video.mp4')
while True:
    # 读取视频每一帧
    hasFrame, frame = cap.read()
    if not hasFrame:
        break
    # 视频长宽
    width = frame.shape[0]
    height = frame.shape[1]
    # 转灰度图
    img_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    # 缩小图片并调整长宽比
    img_resize = cv.resize(img_gray, (int(width / 10), int(height / 10)))

    text = ''
    # 遍历图片中的像素
    for row in img_resize:
        for pixel in row:
            # 根据像素值,选取对应的字符
            text += ascii_char[int(pixel / 256 * char_len)]
        text += '\n'
    # 清屏
    os.system('cls')  # mac是'clear'
    # 输出生成的字符方阵
    print(text)
    # 适当暂停一下
    time.sleep(0.03)

 


8、OpenCV-Python 图像修补 | 六十
https://segmentfault.com/a/1190000022378441?utm_source=sf-related
9、Python中的十大图像处理工具
https://segmentfault.com/a/1190000021328079?utm_source=sf-related
10、OpenCV闯关记——Ubuntu16.04 OpenCV Python开发环境搭建
https://segmentfault.com/a/1190000007715243
11、树莓派入门指北 | 树莓派小无相系列
https://segmentfault.com/a/1190000013996823?utm_source=sf-related
12、树莓派3 + raspbian lite + OpenCV 3 环境搭建
https://segmentfault.com/a/1190000012940611?utm_source=sf-related
13、如何使用python+opencv识别二维码
https://segmentfault.com/a/1190000020885650

 

posted @ 2020-07-05 23:09  wozijisun  阅读(254)  评论(0编辑  收藏  举报