Пример #1
0
img = cv2.drawContours(img, contours, 3, (0,255,0), 3)

But most of the time, below method will be useful:
cnt = contours[4]
img = cv2.drawContours(img, [cnt], 0, (0,255,0), 3)
'''

to_draw = cv2.imread('../../draw_contours.jpg')
to_draw_store = to_draw.copy()
# cv3
# new_img = cv2.drawContours(to_draw, contours,-1, (0,255,0), 3)
# cv2 new_img is None
cv2.drawContours(to_draw, contours, -1, (0, 255, 0), 3)

# ----------------------------------------------------------------------------
plt.subplot(231), plt.imshow(im, cmap='gray'), plt.title('Original')  #
plt.xticks([]), plt.yticks([])
plt.subplot(232), plt.imshow(imgray, cmap='gray'), plt.title('imgray')  #
plt.xticks([]), plt.yticks([])
plt.subplot(233), plt.imshow(thresh_store,
                             cmap='gray'), plt.title('thresh_store')  #
plt.xticks([]), plt.yticks([])
plt.subplot(234), plt.imshow(thresh, cmap='gray'), plt.title('thresh')  #
plt.xticks([]), plt.yticks([])
plt.subplot(235), plt.imshow(to_draw_store,
                             cmap='gray'), plt.title('to_draw_store')  #
plt.xticks([]), plt.yticks([])
plt.subplot(236), plt.imshow(to_draw, cmap='gray'), plt.title('to_draw')  #
plt.xticks([]), plt.yticks([])

plt.show()
dst = cv2.warpPerspective(img, M, (500, 500))
# ---------------------------------------------------------

pts1 = np.float32([[56, 65], [368, 52], [28, 387], [389, 390]])
pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 400]])

M = cv2.getPerspectiveTransform(pts1, pts2)

dst2 = cv2.warpPerspective(img, M, (500, 500))
# ---------------------------------------------------------

pts1 = np.float32([[56, 65], [368, 52], [28, 387], [375, 390]])
pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])

M = cv2.getPerspectiveTransform(pts1, pts2)

dst3 = cv2.warpPerspective(img, M, (500, 500))
# ---------------------------------------------------------

plt.subplot(221), plt.imshow(img), plt.title('perspective Input')
plt.subplot(222), plt.imshow(dst), plt.title('Output1')
plt.subplot(223), plt.imshow(dst2), plt.title('Output2')
plt.subplot(224), plt.imshow(dst3), plt.title('Output3')
plt.show()

cv2.imshow('img', img)
cv2.imshow('dst', dst)
cv2.imshow('dst2', dst2)
cv2.imshow('dst3', dst3)
cv2.waitKey(0)
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-

import cv2
import numpy as np
from matplotlib_1 import pyplot as plt
img = cv2.imread('../opencv_logo.png')
kernel = np.ones((5, 5), np.float32) / 25
#cv.Filter2D(src, dst, kernel, anchor=(-1, -1))
#ddepth –desired depth of the destination image;
#if it is negative, it will be the same as src.depth();
#the following combinations of src.depth() and ddepth are supported:
#src.depth() = CV_8U, ddepth = -1/CV_16S/CV_32F/CV_64F
#src.depth() = CV_16U/CV_16S, ddepth = -1/CV_32F/CV_64F
#src.depth() = CV_32F, ddepth = -1/CV_32F/CV_64F
#src.depth() = CV_64F, ddepth = -1/CV_64F
#when ddepth=-1, the output image will have the same depth as the source.
dst = cv2.filter2D(img, -1, kernel)
plt.subplot(121), plt.imshow(img), plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(dst), plt.title('Averaging')
plt.xticks([]), plt.yticks([])
plt.show()
Пример #4
0
import numpy as np
from matplotlib_1 import pyplot as plt

BLUE = [255, 0, 0]

img1 = cv2.imread(
    'C:/Users/JPang3/Desktop/beijing/opencv/opencv_projects/python-opencv/img1.jpg'
)

replicate = cv2.copyMakeBorder(img1, 10, 10, 10, 10, cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img1, 10, 10, 10, 10, cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img1, 10, 10, 10, 10, cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img1, 10, 10, 10, 10, cv2.BORDER_WRAP)
constant = cv2.copyMakeBorder(img1,
                              10,
                              10,
                              10,
                              10,
                              cv2.BORDER_CONSTANT,
                              value=BLUE)

plt.subplot(231), plt.imshow(img1, 'gray'), plt.title('ORIGINAL')
plt.subplot(232), plt.imshow(replicate, 'gray'), plt.title('REPLICATE')
plt.subplot(233), plt.imshow(reflect, 'gray'), plt.title('REFLECT')
plt.subplot(234), plt.imshow(reflect101, 'gray'), plt.title('REFLECT_101')
plt.subplot(235), plt.imshow(wrap, 'gray'), plt.title('WRAP')
plt.subplot(236), plt.imshow(constant, 'gray'), plt.title('CONSTANT')

plt.show()

# 2行3列第1个
Пример #5
0
# -*- coding: utf-8 -*-
# http://docs.opencv.org/2.4/modules/imgproc/doc/miscellaneous_transformations.html?highlight=cv2.threshold#cv2.threshold

import cv2
import numpy as np
from matplotlib_1 import pyplot as plt

img = cv2.imread('../messi5.jpg', 0)
ret, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
ret, thresh3 = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)
ret, thresh4 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)
ret, thresh5 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)

titles = [
    'Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV'
]
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]

for i in xrange(6):
    plt.subplot(2, 3, i + 1), plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([]), plt.yticks([])

plt.show()