python (+ opencv2 + numpy) for extracting a color image RGB channels, synthesis, and display gray-scale image is calculated


A, image extracted in RGB channels, the code is very simple direct

import CV2

Import sys

import numpy as np

image = cv2.imread(sys.argv[1], cv2.IMREAD_COLOR)

b = image[:, :, 0]

g = image[:, :, 1]

r = image[:, :, 2]

It can be directly used opencv function of split

(b, g, r) = cv2.split(image)

 

Rgb obtained above are one-dimensional matrix, if the direct cv2.imshow ( "b", b), it shows a grayscale (0 ~ 255), to display a corresponding color channel, also need additional the two channels fill 0, how to do it?

(1) may be used cv2 the merge function, such as displaying individual channel b, this can make:

image_b = cv2.merge([b, np.zeros(b.shape, np.uint8), np.zeros(b.shape, np.uint8)])

Wherein np.zeros (b.shape, np.uint8) and b represents the creation of a matrix of the same dimensions, the initial values ​​of each data type is uint8 value 0 and the matrix.

(2) may also be implemented using dstack numpy functions provided:

image_b = np.dstack((b, np.zeros(b.shape, np.uint8), np.zeros(b.shape, np.uint8)))

numpy matrix provides many functions combined calculation. such as:

np.append()

np.concatenate()

np.stack()

np.hstack ()

np.vstack ()

np.dstack ()

Which, concatenate () function is the basis of the function, in fact, * stack () function which is used concatenate () to achieve. These functions are specific what does that mean, go online to check under the bar.

I posted some others finishing:

--------------------------------------------------------

Suppose there are two arrays a, b are:

>>> a

array([0, 1, 2],

       [3, 4, 5],

       [6, 7, 8])

>>> b = a*2

>>> b

array([ 0, 2, 4],

       [ 6, 8, 10],

       [12, 14, 16])

1, the horizontal combination

>>> np.hstack((a,b))

array([ 0, 1, 2, 0, 2, 4],

       [ 3, 4, 5, 6, 8, 10],

       [ 6, 7, 8, 12, 14, 16])

>>> np.concatenate((a,b),axis=1)

array([ 0, 1, 2, 0, 2, 4],

       [ 3, 4, 5, 6, 8, 10],

       [ 6, 7, 8, 12, 14, 16])

2, the vertical combination

>>> np.vstack((a,b))

array([ 0, 1, 2],

       [ 3, 4, 5],

       [ 6, 7, 8],

       [ 0, 2, 4],

       [ 6, 8, 10],

       [12, 14, 16])

>>> np.concatenate((a,b),axis=0)

array([ 0, 1, 2],

       [ 3, 4, 5],

       [ 6, 7, 8],

       [ 0, 2, 4],

       [ 6, 8, 10],

       [12, 14, 16])

3, the depth of the combination: composition along the longitudinal direction

>>> np.dstack((a,b))

array([[ 0, 0],

        [ 1, 2],

        [ 2, 4],

       [ 3, 6],

        [ 4, 8],

        [ 5, 10],

       [ 6, 12],

        [ 7, 14],

        [ 8, 16]])

4, a combination of column column_stack ()

One-dimensional array: column direction composition

Two-dimensional array: As with hstack

5, rows of combined row_stack ()

That array: a combination of a row direction

Two-dimensional array: and as vstack

------------------------------------------------------------

 

Second, the color map is calculated grayscale

opencv which have a direct transfer function, but here the method to implement a custom, calculated grayscale color map Several algorithms have such average value, weighted average value, the maximum value of these algorithms.

The way the average formula:

This simply means that, for each value of each color channel BGR multiplied by 0.333 and adding, as a result of the corresponding pixel gray value.

Weighting scheme equation: value component of each channel is not necessarily, to see how many people like to take the specific

Maximum method, forget, it is estimated to see what great value RGB channel, which will use it.

Python when using the above formula, it is noted, the value of the floating-point matrices, floating point obtained (float32), but is uint8 grayscale value type. This time asytpe function comes in handy, value matrix type conversion.

DETAILED the acquired grayscale code can be found as follows:

gray = 0.114 * b + 0.587 * g + 0.299 * r

gray = gray.astype(np.uint8)

cv2.imshow("gray", gray)

Finally, the detailed code stickers:

import cv2
import sys
import numpy as np
if __name__ == "__main__":
    if len(sys.argv) > 1:
        image = cv2.imread(sys.argv[1], cv2.IMREAD_COLOR)
        b = image[:, :, 0]
        g = image[:, :, 1]
        r = image[:, :, 2]
        #(b g r) = cv2.split(image)
    #image_b = cv2.merge([b, np.zeros(b.shape, np.uint8), np.zeros(b.shape, np.uint8)])
        image_b = np.dstack((b, np.zeros(b.shape, np.uint8), np.zeros(b.shape, np.uint8)))
        cv2.imshow("image_b", image_b)
 
        gray = 0.114 * b + 0.587 * g + 0.299 * r
        gray = gray.astype(np.uint8)
        cv2.imshow("gray", gray)
 
        cv2.imshow ( "Image", Image)
        cv2.waitKey (0)
Specific effects:

Published 86 original articles · won praise 267 · Views 1.77 million +

Guess you like

Origin blog.csdn.net/javastart/article/details/104583862