图像在本地主机上显示已损坏 [英] Image shows up corrupted on localhost

查看:67
本文介绍了图像在本地主机上显示已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 C++ 和一般编程非常陌生.我只是尝试使用 opencv 读取图片并使用 boost asio 在 Web 服务器上显示它.这是我对视频中的所有帧执行此操作之前的初始步骤.以下是我的代码 -

#include #include <字符串>#include #include <线程>#include #include #include #include 使用 boost::asio::ip::tcp;使用命名空间标准;使用命名空间 cv;int main(){尝试{boost::asio::io_service io_service;tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 1112));为了 (;;){tcp::socket 套接字(io_service);接受者.接受(套接字);boost::system::error_code ignore_error;cv::Mat frame = cv::imread("x.jpg");向量<uchar>浅黄色;向量参数=向量(2);参数[0]=cv::IMWRITE_JPEG_QUALITY;参数[1]=95;imencode(".jpg",frame,buff,param);const char mess[] = "axaxaxaxasaaaaaaaaaaxax";std::string 内容(buff.begin(), buff.end());boost::asio::write(socket, boost::asio::buffer(content), boost::asio::transfer_all(), ignore_error);//boost::asio::write(socket, boost::asio::buffer(mess), boost::asio::transfer_all(), ignore_error);}}catch(std::exception& e){std::cerr <<e.what() <<std::endl;}返回0;}

现在发送消息工作正常,但是当我尝试通过内容或 buff 发送图像时,它显示为乱码.我觉得这是因为我在发送图片之前没有发送任何关于图片的信息.但我无法弄清楚如何做到这一点.或者也许我完全错了.任何帮助/建议将不胜感激.干杯!

解决方案

我稍微简化了代码:

#include #include <字符串>#include #include 使用 boost::asio::ip::tcp;int main(){尝试{boost::asio::io_service io_service;tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 1112));为了 (;;){tcp::socket 套接字(io_service);接受者.接受(套接字);cv::Mat frame = cv::imread("x.jpg");std::vector浅黄色;imencode(".jpg", frame, buff, std::vector { cv::IMWRITE_JPEG_QUALITY, 95 });boost::system::error_code 错误;auto bytes_transferred = boost::asio::write(socket, boost::asio::buffer(buff), boost::asio::transfer_all(), err);std::cout <<"写道:<<字节转移<<" (" << err.message() << ")\n";}}catch(std::exception& e){std::cerr <<e.what() <<std::endl;}}

<块引用>

(特别是,不要不必要地使用using namespace,不要不必要地复制到 std::string 中,不要不必要地忽略错误代码)

编译它

g++ test.cpp -L/usr/local/lib -pedantic -Wall -Wextra -pthread -lope^Cv_{core,imgproc,imgcodecs} -lboost_{system,thread} -o test.exe

将 sampe jpeg 复制为 x.jpg,在终端中运行:

./test.exe

然后使用netcat读取结果:

netcat localhost 1112 >验证.jpg

服务器进程每次都会打印相同的消息:

已写:6130(成功)

(6130 字节恰好是我选择的测试图像的 95% 重新编码大小).生成的图像 (verify.jpg) 在我的图像查看器中看起来不错.

结论

我认为代码可能没问题(但请检查上面的改进)并且您可能错误地测试了结果.

I'm horribly new to c++ and programming in general. I'm simply trying to read a picture using opencv and display it on web server using boost asio. This is an initial step before I do this for all frames from a video. The following is my code -

#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <thread>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>

using boost::asio::ip::tcp;
using namespace std;
using namespace cv;

int main(){
    try{
        boost::asio::io_service io_service;
        tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 1112));

        for (;;){
            tcp::socket socket(io_service);
            acceptor.accept(socket);
            boost::system::error_code ignored_error;

            cv::Mat frame = cv::imread("x.jpg");
            vector<uchar> buff;
            vector<int> param = vector<int>(2);
            param[0]=cv::IMWRITE_JPEG_QUALITY;
            param[1]=95;
            imencode(".jpg",frame,buff,param);

            const char mess[] = "axaxaxaxasaaaaaaaaaaxax";

            std::string content(buff.begin(), buff.end());

            boost::asio::write(socket, boost::asio::buffer(content), boost::asio::transfer_all(), ignored_error);
            // boost::asio::write(socket, boost::asio::buffer(mess), boost::asio::transfer_all(), ignored_error);
        }
    }
    catch(std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
    return 0;
}

now sending the message works just fine but when i try to send the image via content or buff, it shows up as gibberish. I feel like it's because I'm not sending any information about the picture prior to sending the picture. But I can't figure out how to do that. Or maybe I'm entirely wrong. Any help/advice would be appreciated. Cheers!

解决方案

I've simplified the code a little:

#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <opencv2/opencv.hpp>

using boost::asio::ip::tcp;

int main(){
    try{
        boost::asio::io_service io_service;
        tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 1112));

        for (;;){
            tcp::socket socket(io_service);
            acceptor.accept(socket);

            cv::Mat frame = cv::imread("x.jpg");
            std::vector<uchar> buff;
            imencode(".jpg", frame, buff, std::vector<int> { cv::IMWRITE_JPEG_QUALITY, 95 });

            boost::system::error_code err;
            auto bytes_transferred = boost::asio::write(socket, boost::asio::buffer(buff), boost::asio::transfer_all(), err);

            std::cout << "Written: " << bytes_transferred << " (" << err.message() << ")\n";
        }
    }
    catch(std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
}

(specifically, don't use using namespace unneccessarily, not copying into a std::string unneccessarily, and not ignoring the error code unnecessarily)

Compiled it with

g++ test.cpp -L/usr/local/lib -pedantic -Wall -Wextra -pthread -lope^Cv_{core,imgproc,imgcodecs} -lboost_{system,thread} -o test.exe

Copied a sampe jpeg as x.jpg, running it in a terminal:

./test.exe

Then using netcat to read the result:

netcat localhost 1112 > verify.jpg

The server process will print the same message each time:

Written: 6130 (Success)

(6130 bytes happens to be the 95% re-encoded size of the test image I chose). The resulting image (verify.jpg) looks fine in my image viewer.

Conclusion

I think the code is probably fine (but check with the improvements above) and you might have been testing the result incorrectly.

这篇关于图像在本地主机上显示已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆