0

I am trying to convert an input image to string and then back to image like a complete cycle. I get Clang-Tidy: Use of a signed integer operand with a binary bitwise operator warning with an unexpected side effect (explained in screenshots below) when I try to specify CV_8UC3 or CV_8UC1 based on my knowledge about the input image. I am not sure what this means?

I am already aware of a similar such question in a different context. But I am not sure how is my case related to that question.

This is my code :

int main (int argc, char *argv[]) {
    bool color = argv[1];
    cv::Mat img = cv::imread("../images/input.jpg", color);
    std::string img_str(img.begin<unsigned char>(), img.end<unsigned char>());
    auto* buffer = (unsigned char *) img_str.c_str();
    // Complaint happens at this line below
    cv::Mat dummy = cv::Mat(img.rows, img.cols, color ? CV_8UC3 : CV_8UC1, buffer);
    cv::imwrite("../images/output.jpg", dummy);
    return 0;
}

Input image:

enter image description here

When color == CV_8UC3:

enter image description here

When color == CV_8UC1

enter image description here

Expected behavior

Color image (replica of input image), when color == CV_8UC3

Grayscale image of input image, when color == CV_8UC1

Arun Kumar
  • 634
  • 2
  • 12
  • 26
  • Does this answer your question? ["Use of a signed integer operand with a binary bitwise operator" - when using unsigned short](https://stackoverflow.com/questions/50399090/use-of-a-signed-integer-operand-with-a-binary-bitwise-operator-when-using-un) – Yunus Temurlenk Mar 05 '20 at 05:58
  • @YunusTemurlenk I am aware of that question. Scanned through it and tried to relate both situations. But I am not able to connect the relationship between that question and my problem. Hence this question is yet not resolved. – Arun Kumar Mar 05 '20 at 06:09
  • It is not an error. But it is a warning given by Clion. My code is fairly large (this segment is a tiny portion of it). I am curious whether this warning creates any dangerous side effects in a computer vision code. – Arun Kumar Mar 05 '20 at 06:12

1 Answers1

1

I solved this issue myself by converting the image <-> string <-> image in an alternative way like below :

std::string ImageToString(const cv::Mat &img) {
    cv::Mat1b linear_img(img.reshape(1));
    return std::string(linear_img.begin(), linear_img.end());
}

After this the converted string/ images are perfect.

Arun Kumar
  • 634
  • 2
  • 12
  • 26