Files
fault-finder/ImageManager.cpp
2020-01-19 14:13:43 -08:00

101 lines
2.8 KiB
C++

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include "FaultFinder.hpp"
#include "ImageManager.hpp"
#define ZERO 0
#define BITS8 0xff
#define BITS14 0x3fff
#define BITS16 0xffff
using namespace cv;
//Mat testFrame = imread("/home/midstate/Documents/flirTest.tif", CV_16UC1);
ImageManager::ImageManager(QObject *parent) : QObject(parent)
{
socket = new QUdpSocket();
socket->connectToHost(QHostAddress::LocalHost, 40000, QIODevice::ReadWrite);
}
ImageManager::~ImageManager()
{
}
void ImageManager::ProcessImage(int width, int height, uint8_t *imgPointer)
{
Mat frame, mask, viewfinder;
std::vector<std::vector<Point>> contours;
static int lower_bound = ZERO;
static int upper_bound = BITS14;
static int temp_threshold = BITS14;
static int fault_area = ZERO;
if (throttle_count == THROTTLE_AMOUNT)
{
throttle_count = 1;
}
else
{
throttle_count++;
return;
}
// Read in frame
//if (!test)
// frame = Mat(height, width, CV_16UC1, imgPointer, Mat::AUTO_STEP);
//else
// frame = testFrame;
frame = Mat(height, width, CV_16UC1, imgPointer, Mat::AUTO_STEP);
// Create mask and then flatten
GaussianBlur(frame, mask, cv::Size(0, 0), 1);
threshold(mask, mask, temp_threshold, BITS16, THRESH_BINARY); // use inrange for mask in range of values
mask.convertTo(mask, CV_8UC1);
// Analyze mask
findContours(mask, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
// Create viewfinder with results
viewfinder = frame.clone();
viewfinder.setTo(lower_bound, viewfinder < lower_bound);
viewfinder.setTo(upper_bound, viewfinder > upper_bound);
normalize(viewfinder, viewfinder, ZERO, BITS8, NORM_MINMAX, CV_8UC1);
applyColorMap(viewfinder, viewfinder, COLORMAP_HOT);
// Apply contours to viewfinder
for(int i = 0; i< contours.size(); i++)
{
double area = contourArea(contours[i]);
if (area > fault_area)
{
// Fault Detected, Draw on Screen and Save to Database
drawContours(viewfinder, contours, i, Scalar(ZERO, BITS8, ZERO), 2);
std::vector<uchar> imageBuffer;
imencode(".pgm", frame, imageBuffer);
QByteArray* imageBytes = new QByteArray(reinterpret_cast<const char*>(imageBuffer.data()), imageBuffer.size());
((FaultFinder*)(parent()))->fault_mgr->SaveImage(imageBytes);
}
}
// Send image to other servers
SendImage(viewfinder);
}
void ImageManager::SendImage(Mat image)
{
std::vector<uchar> buff;
cv::imencode(".jpg", image, buff);
QByteArray *datagram = new QByteArray(reinterpret_cast<const char*>(buff.data()), buff.size());
socket->write(*datagram);
}