Я надеюсь, что кто-то может мне помочь. Я создаю программу на С ++, которая позволяет пользователю нарисовать линию на изображении, которое было снято камерой. Я использую QT и могу заставить пользователя нарисовать линию как бы то ни было; Я хочу видеть линию, как она рисуется пользователем. В настоящее время моя программа позволяет пользователю видеть линию только в том месте, где нажата мышь и когда она отпущена. Я хочу, чтобы программа отображала линию так, как она нарисована.
Я разместил свой код ниже
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <stdio.h>
#include <stddef.h>
#include <QString>
#include <opencv4/opencv2/opencv.hpp>
#include <opencv4/opencv2/core/core.hpp>
#include <opencv4/opencv2/highgui/highgui.hpp>
#include <opencv4/opencv2/imgproc/imgproc.hpp>
#include <opencv4/opencv2/imgcodecs/imgcodecs.hpp>
#include "ueye.h"
#include <QString>
#include <QFileDialog>
#include <QTimer>
#include <QDebug>
using namespace std;
using namespace cv;
HIDS hCam = 0; // 0 for the next available camera. 1-254 to access by ID
SENSORINFO sInfo;
HWND hWndDisplay;
char* pcImageMemory;
int DisplayWidth, DisplayHeight;
cv::Point P3;
cv::Point P4;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
cout << "Lets Begin" << endl;
CaptureImage();
//Timer = new QTimer(this);
//connect(Timer, SIGNAL(timeout()), this, SLOT(DisplayImage()));
//Timer->start(); //Will start the timer
ui->display_image->setMouseTracking(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::CaptureImage(){
cout << "Capture Image" << endl;
is_InitCamera(&hCam, hWndDisplay);
// You can query information about the sensor type used in the camera
is_GetSensorInfo(hCam, &sInfo);
// Saving the information about the max. image proportions in variables
DisplayWidth = sInfo.nMaxWidth;
DisplayHeight = sInfo.nMaxHeight;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Need to find out the memory size of the pixel and the colour mode
int nColorMode;
int nBitsPerPixel = 24;
if (sInfo.nColorMode == IS_COLORMODE_BAYER)
{
// For color camera models use RGB24 mode
nColorMode = IS_CM_BGR8_PACKED;
nBitsPerPixel = 24;
}
else if (sInfo.nColorMode == IS_COLORMODE_CBYCRY)
{
// For CBYCRY camera models use RGB32 mode
nColorMode = IS_CM_BGRA8_PACKED;
nBitsPerPixel = 32;
}
else
{
// For monochrome camera models use Y8 mode
nColorMode = IS_CM_MONO8;
nBitsPerPixel = 24;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int nMemoryId;
// Assigns a memory for the image and sets it active
is_AllocImageMem(hCam, DisplayWidth, DisplayHeight, nBitsPerPixel, &pcImageMemory, &nMemoryId);
is_SetImageMem(hCam, pcImageMemory, nMemoryId);
// Acquires a single image from the camera
is_FreezeVideo(hCam, IS_WAIT);
// Parameter definition for saving the image file
IMAGE_FILE_PARAMS ImageFileParams;
ImageFileParams.pwchFileName = L"./TestImage.bmp"; /// <-- Insert name and location of the image
ImageFileParams.pnImageID = NULL;
ImageFileParams.ppcImageMem = NULL;
ImageFileParams.nQuality = 0;
ImageFileParams.nFileType = IS_IMG_BMP;
// Saves the image file
if (is_ImageFile(hCam, IS_IMAGE_FILE_CMD_SAVE, (void*)&ImageFileParams, sizeof(ImageFileParams)) == IS_SUCCESS)
{
cout << "An Image was saved" << endl;
}
else
{
cout << "something went wrong" << endl;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Releases an image memory that was allocated
is_FreeImageMem(hCam, pcImageMemory, nMemoryId);
// Disables the hCam camera handle and releases the data structures and memory areas taken up by the uEye camera
is_ExitCamera(hCam);
//return -1;
DisplayImage();
}
void MainWindow::DisplayImage(){
cout << "Display image" << endl;
//cv::Point p1(100,100), p2(200,100);
cv::Scalar colorLine(0,255,0); // Green
int thicknessLine = 2;
Mat img;
img = imread("./TestImage.bmp");
cv::resize(img, img, Size(861, 530), 0, 0, INTER_LINEAR);
cv::cvtColor(img,img,cv::COLOR_RGB2BGR); //Qt reads in RGB whereas CV in BGR
//cout << P3 << endl;
cv::line(img, P3, P4, colorLine, thicknessLine);
QImage imdisplay((uchar*)img.data, img.cols, img.rows, img.step, QImage::Format_RGB888); //Converts the CV image into Qt standard format
ui->display_image->setPixmap(QPixmap::fromImage(imdisplay));//display the image in label that is created earlier
}
void MainWindow::mouseMoveEvent(QMouseEvent* event)
{
//qDebug() << "Mouse move.." << event->pos() << "---> " << event->pos().x() << "," << event->pos().y(); // these printing same values
}
void MainWindow::mousePressEvent(QMouseEvent* event)
{
qDebug() << "Mouse press.." << event->pos() << "---> " << event->pos().x() << "," << event->pos().y(); // these printing same values
P3 = cv::Point(event->pos().x(), event->pos().y());
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
qDebug() << "Mouse release.." << event->pos() << "---> " << event->pos().x() << "," << event->pos().y(); // these printing same values
P4 = cv::Point(event->pos().x(), event->pos().y());
DisplayImage();
}
Спасибо