代码之家  ›  专栏  ›  技术社区  ›  Richard Knop

在cvCreateImageHeader之后,JPG图像从30KB变为300KB

  •  1
  • Richard Knop  · 技术社区  · 15 年前

    这是我计划的一部分:

    // let's get jpg image from socket
    int iRcvdBytes=recvfrom(iSockFd, buff, bufferSize, 0, 
    (struct sockaddr*)&cliAddr, (socklen_t*)&cliAddrLen);
    
    // buff now contains 30KB jpg image
    
    // let's load the jpg image to IplImage
    IplImage* fIplImageHeader;
    fIplImageHeader = cvCreateImageHeader(cvSize(640, 480), 8, 1);
    fIplImageHeader->imageData = (char *)buff;
    
    // now let's check the size difference
    cout << "Received " << iRcvdBytes << " bytes from the client" << endl;
    cout << fIplImageHeader->imageSize << endl;
    

    结果是:

    Received 31860 bytes from the client
    307200
    

    为什么呢?cvCreateImageHeader()是在内部将jpg图像转换为RGB还是类似的内容?我希望它保持JPG格式,并用cvShowImage()显示它。

    求求你,欢迎任何帮助。

    2 回复  |  直到 15 年前
        1
  •  2
  •   SingleNegationElimination    15 年前

    您正在比较压缩的jpeg图像数据与未压缩的像素数据的长度。

    特别是,假设:

    fIplImageHeader = cvCreateImageHeader(cvSize(width, height), depth, channels) 
    

    总是这样 fIplImageHeader->imageSize == width * height * (depth/8) * channels

    分配由 recvfrom() 打电话给 imageData 区域一开始就不起作用。

        2
  •  1
  •   Jay    15 年前

    Jpeg不代表图像的精确表示。这是一种“有损”格式(即,您丢失一些细节,以换取较小的图像)。我敢打赌你没有指定你想要的图像的“质量”,所以它使用默认的高质量。查找质量设置并将其设置为较低的值。您需要平衡图像质量和文件大小以适合您的应用程序。