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

Corrupt JPEG data when doing Canny Edge Detection

我正在使用 OpenCV 对相机通过 LAN 获取的图像进行 Canny 边缘检测。 这行得通,但是当我 imshow("recv", canny) 时,我每隔几帧就会在 运行 时间收到 Corrupt JPEG data: premature end of data segment 错误,但是如果我这样做 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;

如果你能得到 mat 形式的 "frame",那么这行可能有问题:

Mat canny = CannyThreshold(frame);

也许你可以用这样的东西来改变这一行:

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