Algomorph/pyboostcvconverter

gray image conversion.

Closed this issue · 4 comments

Hi,

Thanks so much for your projects and it works perfectly for me now. However I got one question, my input image is gray scale, and after the function pbcvt::fromNDArrayToMat , the image becomes 3 channels (RGB).

Is there anyway to modify the conversion function pbcvt::fromNDArrayToMat, so that gray scale image can be still 1 channel after conversion? Because my project requires to process 1 channel gray scale image.

Thanks in advance!

Regards,
Weibo Qiu.

Hi, @qiuweibo!

I just wrote this function:

/**
 * @brief Example function. Simply prerforms the conversion and returns it back as a new array. Doesn't make any changes.
 * @return The resulting numpy array.
 */
PyObject* convert(PyObject* py_image_in) {
	cv::Mat image = pbcvt::fromNDArrayToMat(py_image_in);
	PyObject * py_image = pbcvt::fromMatToNDArray(image);
	return py_image;
}

I get this output in python:

In [11]: array = (np.random.rand(3,3) * 200).astype(np.uint8)

In [12]: array
Out[12]: 
array([[ 65, 154,  86],
       [ 51,  86,  81],
       [160, 109,  29]], dtype=uint8)

In [13]: pbcvt.convert(array)
Out[13]: 
array([[ 65, 154,  86],
       [ 51,  86,  81],
       [160, 109,  29]], dtype=uint8)

So, it seems like it's converting to the correct type and channel number.

I did this for python3 / OpenCV3.
Does this same function return the same output for you as well? Also, what Python / OpenCV versions are you using?

Hi @Algomorph ,

Thanks so much for your quick reply!
I just tested it and it works fine.

I used python to read an colorful image and use cv2_Color_BGR2GRAY function to transfer it into gray image. Then I used your function pbcvt::fromNDArrayToMat and the output is still 1 channel.

I also tried to directly input a 3 channle colorful image to your conversion function and the output is 3 channels. Therefore the conversion function is consistent:

1 channel image input -> 1 channel image output
3 channels image input -> 3 channels image output

I am using Python3, OpenCV 3.4.0, Boost 1.68

Thanks so much.
Regards,
Weibo.

@qiuweibo , if the output is consistent, then I'm not sure I understand... What is the problem?

@Algomorph

Hi, I am sorry. It was my problem. The first image I input was a 3-channel gray image. But I thought it would be 1-channel since it is gray scale.

Now, everything is fine. Thanks for your help, and sorry for my mistakes.