0

I am reading the QByteArray using QTcpSocket and converting the array into the cvMat image. to display the image using imshow().but i am getting gray image. code is as follows.

//array ->QBytearray (received from socket) 
cv::Mat img,img1;
    img.cols=320;
    img.rows=240;
img = cv::Mat(240,320, CV_8UC1,array.data());
cv::cvtColor(img, img, CV_GRAY2RGB);  // 
      cv::imshow("image display",img);
      cv::waitKey(5000);

after cvtColour() function also its not converting into colour image.

Thanks in advance.

Ilian Zapryanov
  • 1,132
  • 2
  • 16
  • 28
Pooja
  • 33
  • 1
  • 7
  • Look at `cv::Mat::convertTo`. In `cvtColor` src and dest have the same depth. – rafix07 Dec 23 '20 at 10:44
  • Test it with CV_8UC3 – Farshid616 Dec 23 '20 at 10:53
  • @rafix07 after changing src and destination also showing same gray image. – Pooja Dec 23 '20 at 10:55
  • 1
    Please show the code that serializes and sends the image. – G.M. Dec 23 '20 at 10:57
  • @Farshid616 it throws an error. OpenCV Error: Assertion failed (scn == 1 && (dcn == 3 || dcn == 4)) in cvtColor, file /build/opencv-ys8xiq/opencv-2.4.9.1+dfsg/modules/imgproc/src/color.cpp, line 3789 terminate called after throwing an instance of 'cv::Exception' what(): /build/opencv-ys8xiq/opencv-2.4.9.1+dfsg/modules/imgproc/src/color.cpp:3789: error: (-215) scn == 1 && (dcn == 3 || dcn == 4) in function cvtColor Aborted (core dumped) – Pooja Dec 23 '20 at 10:57
  • @G.M. Image send code i have implement on linux platform using opencv c++ . ``` Mat image = imread("pk.png",0); int sockfd, portno, n, imgSize, IM_HEIGHT, IM_WIDTH; imgSize = image.total()*image.elemSize(); n = send(sockfd, image.data,imgSize, 0); printf("n=%d\n",n); if (n < 0) error("ERROR writing to socket"); ``` – Pooja Dec 23 '20 at 11:01
  • CV_GRAY2RGB - > COLOR_GRAY2RGB ? – Hihikomori Dec 23 '20 at 11:02
  • @Hihikomori COLOR_GRAY2RGB its not supported. – Pooja Dec 23 '20 at 11:05
  • cv::cvtColor(img, img, CV_GRAY2RGB) - copies one channel image to channels of rgb image. After converting it's still will by looking gray. You need to modify one channel to see some color. – Hihikomori Dec 23 '20 at 11:11
  • @Hihikomori could you please suggest how to modify one channel . I have tried many ways buts its not working. Thanks in advance – Pooja Dec 23 '20 at 11:16
  • Test it with CV_8UC3 also remove cvtcolor line or change CV_GRAY2RGB with CV_BGR2RGB – Farshid616 Dec 23 '20 at 11:17
  • @Farshid616 Throws and segmentation fault received signal SIGSEGV, Segmentation fault. 0x00007ffff73b7640 in cv::CvtColorLoop_Invoker >::operator()(cv::Range const&) const () from /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4 – Pooja Dec 23 '20 at 11:23
  • You are reading from socket grayscale image. There is no method to make it colored back. – Hihikomori Dec 23 '20 at 13:50

1 Answers1

0

This is the way to modify channels separately:

img = cv::Mat(240,320, CV_8UC1,array.data());
cv::Mat img1;
cv::divide(img,cv::Scalar(2),img1);
std::vector<cv::Mat> channels;
   
channels.push_back(img);
channels.push_back(img1);
channels.push_back(img);
cv::merge(channels, img);

cv::imshow("image display",img);
cv::waitKey(5000);
Hihikomori
  • 950
  • 6
  • 12
  • Thanks for prompt help. but there is difference between original client image and server QT image. @server side it showing in only one colour. – Pooja Dec 23 '20 at 11:44
  • Is it good approach to convert the cv mat image into Qimage and then display in QT – Pooja Dec 23 '20 at 12:23