日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python opencv2_Python + OpenCV2 系列:2 - 图片操作

發(fā)布時(shí)間:2025/3/11 python 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python opencv2_Python + OpenCV2 系列:2 - 图片操作 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

這些相當(dāng)于我的學(xué)習(xí)筆記,所以并沒(méi)有很強(qiáng)的結(jié)構(gòu)性和很全的介紹,請(qǐng)見(jiàn)諒。

1. 讀取/寫入圖像

下面是一個(gè)簡(jiǎn)短的載入圖像、打印尺寸、轉(zhuǎn)換格式及保存圖像為.png的例子:

#-*- coding: utf-8 -*-

importcv2

import numpy as np#讀入圖像

im = cv2.imread('../data/empire.jpg')#打印圖像尺寸

h, w = im.shape[:2]printh, w#保存原jpg格式的圖像為png格式圖像

cv2.imwrite('../images/ch10/ch10_P210_Reading-and-Writing-Images.png',im)

# 注:imread默認(rèn)讀取的是RGB格式,所以即使原圖像是灰度圖,讀出來(lái)仍然是三個(gè)通道,所以,在imread之后可以添加參數(shù)

# 注:這里是相對(duì)路徑: \與/是沒(méi)有區(qū)別的,‘’ 和 “” 是沒(méi)有區(qū)別的。 ../表示返回到上一級(jí)目錄下,./表示與該源碼文件同一級(jí)目錄下。

"\"這種斜杠使用需要用轉(zhuǎn)義字符,即"\\"表示單“\”。而“/” 不需要轉(zhuǎn)義字符,即單個(gè)斜杠就可以了。所以在使用時(shí),形式如下:

im = cv2.imread('../data/empire.jpg')

im= cv2.imread('..\\data\\empire.jpg')

# 注:函數(shù)imread()將圖像返回為一個(gè)標(biāo)準(zhǔn)的NumPy數(shù)組。

1.1 相關(guān)注釋

cv2.imread

Python:cv2.imread(filename[, flags])

Parameters:

filename?– Name of file to be loaded.

flags?–

Flags specifying the color type of a loaded image:

CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.

CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one

CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one

>0?Return a 3-channel color image.Note

In the current implementation the alpha channel, if any, is stripped from the output image. Use negative value if you need the alpha channel.

=0?Return a grayscale image. 如果是灰度圖就用這個(gè)就好了。例如:cv2.imread'../data/empire.jpg',0)

<0?Return the loaded image as is (with alpha channel).

cv2.imwrite

Python:cv2.imwrite(filename, img[, params])

Parameters:

filename?– Name of the file.

image?– Image to be saved.

params?–

Format-specific save parameters encoded as pairs?paramId_1,?paramValue_1,?paramId_2,?paramValue_2,?...?. The following parameters are currently supported:

For JPEG, it can be a quality (?CV_IMWRITE_JPEG_QUALITY?) from 0 to 100 (the higher is the better). Default value is 95.

For PNG, it can be the compression level (?CV_IMWRITE_PNG_COMPRESSION?) from 0 to 9. A higher value means a smaller size and longer compression time. Default value is 3.

For PPM, PGM, or PBM, it can be a binary format flag (?CV_IMWRITE_PXM_BINARY?), 0 or 1. Default value is 1.

2.圖像RGB/HSV 通道分離

#Convert BGR to r,g,b

b,g,r =cv2.split(im)#Convert BGR to HSV

image_hue_saturation_value =cv2.cvtColor(im, cv2.COLOR_BGR2HSV)

h,s,v=cv2.split(image_hue_saturation_value)#Convert BGR to gray

image_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

# 注:RGB channels is indexed in B G R which is different from matlab。

# 注:Any channels could be split using cv2.split, pay attention to the sequence of channels

2.1 相關(guān)注釋

Python:cv2.split(m[, mv])?→ mv

Parameters:

src?– input multi-channel array.

mv?– output array or vector of arrays; in the first variant of the function the number of arrays must match?src.channels(); the arrays themselves are reallocated, if needed.

Python:cv2.cvtColor(src, code[, dst[, dstCn]])?→ dst

Parameters:

src?– input image: 8-bit unsigned, 16-bit unsigned (?CV_16UC...?), or single-precision floating-point.

dst?– output image of the same size and depth as?src.

code?– color space conversion code (see the description below).

dstCn?– number of channels in the destination image; if the parameter is 0, the number of the channels is derived automatically from?src?and?code?.

3.圖像矩陣的操作(點(diǎn)乘,復(fù)制,截取,1到N維矩陣)

# mask seed 3D matrix

seed_mask_single_channel_list = np.array([[[1,0,0],[0,0,0],[0,0,0]],[[0,1,0],[0,0,0],[0,0,0]],[[0,0,1],[0,0,0],[0,0,0]],

[[0,0,0],[1,0,0],[0,0,0]],[[0,0,0],[0,1,0],[0,0,0]],[[0,0,0],[0,0,1],[0,0,0]],

[[0,0,0],[0,0,0],[1,0,0]],[[0,0,0],[0,0,0],[0,1,0]],[[0,0,0],[0,0,0],[0,0,1]]])

#cut image

image_new_sample = image_source[:200,:200] #取前200個(gè)行和列的元素,python是從0開(kāi)始的,所以0:200表示的是0-199這200個(gè)元素,取不到200.而初始位置0可以省略#separate channel

mask_singel_channel = np.tile(seed_mask_single_channel_list[1],(70,70))[:200,:200] #第一個(gè)3*3的mask作為一個(gè)單元進(jìn)行復(fù)制成為70行,70列,截取前200行,200列

single_channel_image= mask_singel_channel * image_new_sample #表示點(diǎn)乘

# 注:矩陣的操作用Numpy這個(gè)類庫(kù)進(jìn)行。

3.1 相關(guān)注釋

numpy.array(object,?dtype=None,?copy=True,?order=None,?subok=False,?ndmin=0)

Parameters:

object?: array_like

An array, any object exposing the array interface, an object whose __array__ method returns an array, or any (nested) sequence.

dtype?: data-type, optional

The desired data-type for the array. If not given, then the type will be determined as the minimum type required to hold the objects in the sequence. This argument can only be used to ‘upcast’ the array. For downcasting, use the .astype(t) method.

copy?: bool, optional

If true (default), then the object is copied. Otherwise, a copy will only be made if __array__ returns a copy, if obj is a nested sequence, or if a copy is needed to satisfy any of the other requirements (dtype,?order, etc.).

order?: {‘C’, ‘F’, ‘A’}, optional

Specify the order of the array. If order is ‘C’ (default), then the array will be in C-contiguous order (last-index varies the fastest). If order is ‘F’, then the returned array will be in Fortran-contiguous order (first-index varies the fastest). If order is ‘A’, then the returned array may be in any order (either C-, Fortran-contiguous, or even discontiguous).

subok?: bool, optional

If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array (default).

ndmin?: int, optional

Specifies the minimum number of dimensions that the resulting array should have. Ones will be pre-pended to the shape as needed to meet this requirement.

Returns:

out?: ndarray

An array object satisfying the specified requirements.

e.g. 最外層始終都是[],所以如果是1維就一個(gè)[],2維就2個(gè),N維就N個(gè)

>>> np.array([1, 2, 3])

array([1, 2, 3])>>> np.array([[1, 2], [3, 4]])

array([[1, 2],

[3, 4]])>>> np.array([1, 2, 3], ndmin=2)

array([[1, 2, 3]])

numpy.tile(A,?reps)

Parameters:

A?: array_like

The input array.

reps?: array_like

The number of repetitions of?A?along each axis.

Returns:

c?: ndarray

The tiled output array

e.g.

>>> b = np.array([[1, 2], [3, 4]])>>> np.tile(b, 2)

array([[1, 2, 1, 2],

[3, 4, 3, 4]])>>> np.tile(b, (2, 1))

array([[1, 2],

[3, 4],

[1, 2],

[3, 4]])

總結(jié)

以上是生活随笔為你收集整理的python opencv2_Python + OpenCV2 系列:2 - 图片操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。