进行Canny边缘检测时JPEG数据损坏

时间:2019-05-02 19:11:58

标签: c++ opencv jpeg realsense

我正在使用OpenCV对通过LAN的​​摄像头获得的图像进行Canny Edge检测。 这行得通,但是我一直在获取损坏的JPEG数据:imshow("recv", canny)时每隔几帧,运行时数据段错误就会提前结束,但是如果我imshow("recv", frame)

// Block until receive message from a client
do {
    recvMsgSize = sock.recvFrom(buffer, BUF_LEN, sourceAddress, sourcePort);
} while (recvMsgSize > sizeof(int));
int total_pack = ((int*)buffer)[0];

char* longbuf = new char[double(PACK_SIZE * total_pack)];
for (int i = 0; i < total_pack; i++) {
    recvMsgSize = sock.recvFrom(buffer, BUF_LEN, sourceAddress, sourcePort);
         if (recvMsgSize != PACK_SIZE) {
         continue;
    }
    memcpy(&longbuf[i * PACK_SIZE], buffer, PACK_SIZE);
}

Mat rawData = Mat(1, PACK_SIZE * total_pack, CV_8UC1, longbuf);
Mat frame = imdecode(rawData, IMREAD_COLOR);
if (frame.size().width == 0) {
    continue;
}
Mat canny = CannyThreshold(frame);
imshow("recv", canny);
delete[] longbuf;

1 个答案:

答案 0 :(得分:0)

如果您可以使用垫子形式的“框架”,则此行可能有问题:

Mat canny = CannyThreshold(frame);

也许您可以使用以下内容更改此行:

int minCannyThreshold = 190;
int maxCannyThreshold = 230;
Canny(frame, frame, minCannyThreshold, maxCannyThreshold, 5, true);
imshow("recv", frame);