opencv, Image module questions and answers on Single and three-channel mutual conversion

opencv, Image module questions and answers on Single and three-channel mutual conversion

I. Introduction

More recent project with pictures, sometimes forget to change the default value so that the API reading has become a three-channel grayscale chart, although after the change over, but very curious difference between the results from the original reading of both the late transformation. In the hands-on multi-toss ideas, pumping a good time to experiment a bit with the following results.

Second, the test

1.opencv module

Where the prior codes, talk results, as follows:

src_1 = cv2.imread(r"F:\demo\MS_DMS\add\0_\2.jpg", 0) # 正确读入单通道的方式
src_3 = cv2.imread(r"F:\demo\MS_DMS\add\0_\2.jpg") # 以三通道的方式读入
print(src_1)
gray = cv2.cvtColor(src_3, cv2.COLOR_BGR2GRAY) # 再将后者由三通道转换为单通道
list_ = ((gray == src_1).astype("int8")).tolist() # 转为列表,方便计算比对结果中 0(False) 的个数
print(list_.count(0))

Results

src_1 = 
[[148 149 150 ... 154 154 154]
 [148 149 150 ... 154 154 154]
 [148 150 151 ... 154 154 154]
 ...
 [154 146 132 ... 213 195 154]
 [144 135 130 ... 172 207 169]
 [134 123 118 ... 210 180 199]]
 
list_.count(0) = 0 # 说明两个数组相等

As can be seen from the above, the original is read and press the three-channel grayscale FIG read, and then converted back to single-channel embodiment of FIG compared, the result is equal to two. Look at, is how a single channel into a three-channel change.

src_1 =  [[148 149 150 ... 154 154 154]
 [148 149 150 ... 154 154 154]
 [148 150 151 ... 154 154 154]
 ...
 [154 146 132 ... 213 195 154]
 [144 135 130 ... 172 207 169]
 [134 123 118 ... 210 180 199]]

src_3 =  [[[148 148 148]
  [149 149 149]
  [150 150 150]
  ...
  [154 154 154]
  [154 154 154]
  [154 154 154]]

 [[148 148 148]
  [149 149 149]
  [150 150 150]
  ...
  [154 154 154]
  [154 154 154]
  [154 154 154]]

 [[148 148 148]
  [150 150 150]
  [151 151 151]
  ...
  [154 154 154]
  [154 154 154]
  [154 154 154]]

 ...

 [[154 154 154]
  [146 146 146]
  [132 132 132]
  ...
  [213 213 213]
  [195 195 195]
  [154 154 154]]

 [[144 144 144]
  [135 135 135]
  [130 130 130]
  ...
  [172 172 172]
  [207 207 207]
  [169 169 169]]

 [[134 134 134]
  [123 123 123]
  [118 118 118]
  ...
  [210 210 210]
  [180 180 180]
  [199 199 199]]]

src_3[0:1, 0:1, :] = [[[148 148 148]]] # 打印了三通道图的第一个像素点的数值看了一下

Long, roughly look on the line, the program is to simply copy a single channel of data about that the other two data channels with another, this one from the first pixel point I deliberately printed value can be seen. Like we pytorch, the shape of the array is (C, H, W), that is what is it?

# 特意将数组形状转换了一下,数值表现如下:
src_3_ =  [[[148 148 148 ... 152 152 152]
  [152 152 149 ... 151 152 152]
  [152 152 152 ... 154 154 154]
  ...
  [157 157 157 ... 157 157 157]
  [150 150 150 ... 172 172 191]
  [191 191 208 ... 151 158 158]]

 [[158 161 161 ... 155 155 155]
  [150 150 150 ... 203 203 213]
  [213 213 173 ... 144 158 158]
  ...
  [ 96  96  68 ... 134 151 151]
  [151 129 129 ... 163 163 163]
  [142 142 142 ...  84  84  74]]

 [[ 74  74  94 ... 122 115 115]
  [115  95  95 ... 164 164 164]
  [140 140 140 ... 110 110 116]
  ...
  [134 134 134 ... 163 163 163]
  [163 163 163 ... 165 166 166]
  [166 166 166 ... 199 199 199]]]
  
src_3_.shape =  (3, 74, 76)

2.Image module

ptyorch in this module are more, so also experiment a bit.

src = Image.open(r"F:\demo\MS_DMS\add\0_\2.jpg")
print(np.array(src))

The results are:

[[[148 148 148]
  [149 149 149]
  [150 150 150]
  ...
  [154 154 154]
  [154 154 154]
  [154 154 154]]

 [[148 148 148]
  [149 149 149]
  [150 150 150]
  ...
  [154 154 154]
  [154 154 154]
  [154 154 154]]

 [[148 148 148]
  [150 150 150]
  [151 151 151]
  ...
  [154 154 154]
  [154 154 154]
  [154 154 154]]

 ...

 [[154 154 154]
  [146 146 146]
  [132 132 132]
  ...
  [213 213 213]
  [195 195 195]
  [154 154 154]]

 [[144 144 144]
  [135 135 135]
  [130 130 130]
  ...
  [172 172 172]
  [207 207 207]
  [169 169 169]]

 [[134 134 134]
  [123 123 123]
  [118 118 118]
  ...
  [210 210 210]
  [180 180 180]
  [199 199 199]]]

Image of the open function is not provided by grayscale reading (read the documentation of the function did not find anything), and then only late convert it to a single channel map, so pytorch read single channel view when the general would go convert it. Back to results above, this is consistent with the results of opencv, do not believe you can make a comparison.

src2 = cv2.imread(r"F:\demo\MS_DMS\add\0_\2.jpg")
src = Image.open(r"F:\demo\MS_DMS\add\0_\2.jpg")
print(((src2 == np.array(src).astype("int8")).tolist()).count(0))

结果为 0 ,说明以不同API读取的两个数组结果是一样的。

In addition, Image is the single channel into a three-channel data directly on a channel and then two copies, so the same three-channel data.
Try the following results single channel.

src2 = cv2.imread(r"F:\demo\MS_DMS\add\0_\2.jpg", 0) # opencv按单通道读取图片
src = Image.open(r"F:\demo\MS_DMS\add\0_\2.jpg").convert("L") # Image将三通道转为单通道
print("src = ",np.array(src))
print(((src2 == np.array(src).astype("int8")).tolist()).count(0)) # 比对二者是否一样

结果为:

src =  [[148 149 150 ... 154 154 154]
 [148 149 150 ... 154 154 154]
 [148 150 151 ... 154 154 154]
 ...
 [154 146 132 ... 213 195 154]
 [144 135 130 ... 172 207 169]
 [134 123 118 ... 210 180 199]]
 
((src2 == np.array(src).astype("int8")).tolist()).count(0) =  0    # 为 0 表示上面两个数组是相等的

Third, the summary

After this test, and finally a solution of doubts in my mind, to sum up:
① OpenCV according to the results of a single-channel read pictures and read the press picture and then converted a three-channel single-channel and consistent results of both;
② Image only by three-channel read the image, opencv consistent with the results of a three-channel way it reads. After conversion to a single channel, the result is also consistent with opencv.
And there are other possible doubt, test it again slowly. Comfortable ~ ~

发布了11 篇原创文章 · 获赞 1 · 访问量 1057

Guess you like

Origin blog.csdn.net/tangshopping/article/details/103995485