Skip to content

Commit

Permalink
image filter added
Browse files Browse the repository at this point in the history
  • Loading branch information
ClemensElflein committed Jun 18, 2019
1 parent 593c336 commit cc8ea4b
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ src/protobuf/**
ehthumbs.db
Thumbs.db

filter_test_input
filter_test_output
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ add_executable(self_o_mat src/main.cpp src/logic/BoothLogic.cpp src/logic/BoothL
src/camera/controllers/TriggerController.h src/camera/controllers/BaseController.cpp src/camera/controllers/FocusController.cpp
src/camera/controllers/FocusController.h src/camera/controllers/InfoController.cpp src/camera/controllers/InfoController.h
src/camera/controllers/ApertureController.h src/camera/controllers/ExposureCorrectionController.h src/camera/controllers/ChoiceRangeController.cpp
src/camera/controllers/ChoiceRangeController.h src/tools/readfile.h src/tools/readfile.cpp src/tools/cobs.h src/tools/cobs.cpp src/logic/SelfomatController.cpp src/logic/SelfomatController.h)
src/camera/controllers/ChoiceRangeController.h src/tools/readfile.h src/tools/readfile.cpp src/tools/cobs.h src/tools/cobs.cpp src/logic/SelfomatController.cpp src/logic/SelfomatController.h src/logic/IImageFilter.h src/logic/BasicImageFilter.cpp src/logic/BasicImageFilter.h src/logic/IImageFilter.cpp)



target_link_libraries(self_o_mat ${Boost_LIBRARIES})
target_link_libraries(self_o_mat gphoto2)
Expand All @@ -73,6 +75,11 @@ target_link_libraries(self_o_mat ${CUPS_LIBRARIES})
target_link_libraries(self_o_mat served)
target_link_libraries(self_o_mat protobuf)

add_executable(filter_test src/main_filter.cpp src/logic/IImageFilter.h src/logic/BasicImageFilter.cpp src/logic/BasicImageFilter.h src/logic/IImageFilter.cpp)
target_link_libraries(filter_test ${Boost_LIBRARIES})
target_link_libraries(filter_test ${ImageMagick_LIBRARIES})


if(USE_SPI)
target_link_libraries(self_o_mat wiringPi)
endif()
Expand Down
2 changes: 1 addition & 1 deletion settings/dev.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"camera_type": "nop",
"camera_type": "opencv",
"has_flash": false,
"has_button": true,
"disable_watchdog": false,
Expand Down
2 changes: 1 addition & 1 deletion src/camera/ICamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace selfomat {

virtual void stop() = 0;

/**
virtual /**
* Returns the current state of the camera
* @return
*/
Expand Down
8 changes: 8 additions & 0 deletions src/camera/OpenCVCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using namespace selfomat::camera;

CameraStartResult OpenCVCamera::start() {


if (!cap.open(0)) {
cout << "CVCamera: Error opening camera" << endl;
return START_RESULT_ERROR;
Expand Down Expand Up @@ -184,4 +185,11 @@ bool OpenCVCamera::setExposureCorrectionTrigger(int exposure_correction_choice)
return false;
}

CameraState OpenCVCamera::getState() {
if(cap.isOpened()) {
return CameraState::STATE_WORKING;
}
return CameraState::STATE_ERROR;
}


1 change: 1 addition & 0 deletions src/camera/OpenCVCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace selfomat {

int getShootingMode() override;

CameraState getState() override;

bool setIso(int iso_choice) override;

Expand Down
22 changes: 22 additions & 0 deletions src/logic/BasicImageFilter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Created by clemens on 18.06.19.
//

#include "BasicImageFilter.h"

using namespace selfomat::logic;

void BasicImageFilter::processImage(Magick::Image &image, double gain) {
image.normalize();
image.autoGamma();

image.sigmoidalContrast(0, interpolate(1.0, 2.5, gain));

image.modulate(interpolate(100.0, 120.0, gain),
interpolate(100.0, 120.0, gain),
100.0);
}

std::string BasicImageFilter::getName() {
return "basic_image_filter";
}
24 changes: 24 additions & 0 deletions src/logic/BasicImageFilter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Created by clemens on 18.06.19.
//

#ifndef SELF_O_MAT_BASICIMAGEFILTER_H
#define SELF_O_MAT_BASICIMAGEFILTER_H


#include "IImageFilter.h"

namespace selfomat {
namespace logic {
class BasicImageFilter : public IImageFilter {
public:
std::string getName() override;

void processImage(Magick::Image &image, double gain) override;
};

}
}


#endif //SELF_O_MAT_BASICIMAGEFILTER_H
4 changes: 2 additions & 2 deletions src/logic/BoothLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,9 @@ void BoothLogic::printerThread() {

Magick::Image toPrepare;
if (templateEnabled) {
toPrepare = imageProcessor.frameImageForPrint(latestJpegBuffer, latestJpegBufferSize);
toPrepare = imageProcessor.frameImageForPrint(latestJpegBuffer, latestJpegBufferSize, BASIC_FILTER);
} else {
toPrepare = imageProcessor.decodeImageForPrint(latestJpegBuffer, latestJpegBufferSize);
toPrepare = imageProcessor.decodeImageForPrint(latestJpegBuffer, latestJpegBufferSize, BASIC_FILTER);
}
cout << "[Printer Thread] " << "Framed" << endl;
printerManager.prepareImageForPrint(toPrepare);
Expand Down
10 changes: 10 additions & 0 deletions src/logic/IImageFilter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Created by clemens on 18.06.19.
//

#include "IImageFilter.h"

double selfomat::logic::IImageFilter::interpolate(double min, double max, double percentage) {
return (max - min)*percentage + min;
}

25 changes: 25 additions & 0 deletions src/logic/IImageFilter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Created by clemens on 18.06.19.
//

#ifndef SELF_O_MAT_IIMAGEFILTER_H
#define SELF_O_MAT_IIMAGEFILTER_H

#include <Magick++.h>
#include <Magick++/Image.h>


namespace selfomat {
namespace logic {
class IImageFilter {
protected:
double interpolate(double min, double max, double percentage);
public:
virtual std::string getName() = 0;
virtual void processImage(Magick::Image &image, double gain) = 0;
};
}
}


#endif //SELF_O_MAT_IIMAGEFILTER_H
23 changes: 20 additions & 3 deletions src/logic/ImageProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ ImageProcessor::~ImageProcessor() {
}


Image ImageProcessor::frameImageForPrint(void *inputImageJpeg, size_t jpegBufferSize) {
Image ImageProcessor::frameImageForPrint(void *inputImageJpeg, size_t jpegBufferSize, FILTER filter) {

if(!templateLoaded)
return decodeImageForPrint(inputImageJpeg, jpegBufferSize);
return decodeImageForPrint(inputImageJpeg, jpegBufferSize, filter);

struct timespec tstart, tend;

Expand Down Expand Up @@ -176,14 +176,16 @@ Image ImageProcessor::frameImageForPrint(void *inputImageJpeg, size_t jpegBuffer

result.profile("ICC", sRgbIcc);

applyFilter(result, filter);

return result;
}

bool ImageProcessor::stop() {
return true;
}

Image ImageProcessor::decodeImageForPrint(void *inputImageJpeg, size_t jpegBufferSize) {
Image ImageProcessor::decodeImageForPrint(void *inputImageJpeg, size_t jpegBufferSize, FILTER filter) {
struct timespec tstart, tend;


Expand Down Expand Up @@ -240,6 +242,8 @@ Image ImageProcessor::decodeImageForPrint(void *inputImageJpeg, size_t jpegBuffe
inputImageMagic.iccColorProfile(adobeRgbIcc);
}

applyFilter(inputImageMagic, filter);

return inputImageMagic;
}

Expand Down Expand Up @@ -387,3 +391,16 @@ bool ImageProcessor::updateTemplate(void *data, size_t size) {

return true;
}

void ImageProcessor::applyFilter(Image image, FILTER filter) {
image.write("before_filter.jpg");
switch(filter) {
case BASIC_FILTER:
std::cout << "basic filter" << std::endl;
basicFilter.processImage(image);
break;
default:
break;
}
image.write("after_filter.jpg");
}
19 changes: 17 additions & 2 deletions src/logic/ImageProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@
#include <tools/JpegDecoder.h>
#include <easyexif/exif.h>
#include <opencv2/opencv.hpp>
#include "BasicImageFilter.h"


using namespace Magick;
using namespace selfomat::tools;

namespace selfomat {
namespace logic {

enum FILTER {
NO_FILTER = 0,
BASIC_FILTER = 1
};

class ImageProcessor {
private:
struct Rect {
Expand Down Expand Up @@ -53,11 +61,15 @@ namespace selfomat {
Rect getOffset(Image *image, int accuracy = 1);
void writeOffset(Rect offset, std::string filename);

BasicImageFilter basicFilter;

void applyFilter(Image image, FILTER filter);

public:
explicit ImageProcessor(ILogger *logger);

Image frameImageForPrint(void *inputImageJpeg, size_t jpegBufferSize);
Image decodeImageForPrint(void *inputImageJpeg, size_t jpegBufferSize);
Image frameImageForPrint(void *inputImageJpeg, size_t jpegBufferSize, FILTER filter = NO_FILTER);
Image decodeImageForPrint(void *inputImageJpeg, size_t jpegBufferSize, FILTER filter = NO_FILTER);

bool start();

Expand All @@ -70,6 +82,9 @@ namespace selfomat {
}

bool updateTemplate(void *data, size_t size);



};
}
}
Expand Down
62 changes: 62 additions & 0 deletions src/main_filter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <vector>
#include <logic/IImageFilter.h>
#include <logic/BasicImageFilter.h>
#include <iostream>
#include <boost/filesystem.hpp>
#include <string>

using namespace selfomat::logic;
using namespace boost::filesystem;

int main(int argc, char *argv[]) {

if(argc != 2) {
std::cout << "Usage: ./filter_test <image in dir> <image out dir>" << std::endl;
return 1;
}

std::vector<IImageFilter*> filters;
filters.push_back(new BasicImageFilter());

path p(argv[1]);
directory_iterator end_itr;

path out_dir("filter_test_output");

system("rm -rf ./filter_test_output");

create_directories(out_dir);



// cycle through the directory
int imageIndex = 0;
for (directory_iterator itr(p); itr != end_itr; ++itr)
{
if (is_regular_file(itr->path())) {
imageIndex++;
std::string current_file = itr->path().string();
std::cout << current_file << std::endl;

// Load file into image magick
Magick::Image image;


for(auto &filter : filters) {
for(double d = 0.0; d <= 1.0; d+=0.25) {
image.read(current_file);
image.resize(Magick::Geometry(1000, 1000));
image.write("scaled.jpg");
filter->processImage(image, d);
image.write("filtered.jpg");
std::string outfile =
out_dir.string() + "/" + std::to_string(imageIndex) + "_" + filter->getName() + "_" + std::to_string((int)(d*100)) +
itr->path().extension().string();
system((std::string("convert +append scaled.jpg filtered.jpg ") + outfile).c_str());
//system((std::string("mv filtered.jpg ")+outfile).c_str());
}
}

}
}
}

0 comments on commit cc8ea4b

Please sign in to comment.