Multithreading

This commit is contained in:
Andrey Tkachenko 2014-07-07 16:07:47 +04:00
parent 1d08b711b0
commit 6ab5abb3e7
23 changed files with 130 additions and 222 deletions

Binary file not shown.

View File

@ -1,59 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
-include ../makefile.init
RM := rm -rf
# All of the sources participating in the build are defined here
-include sources.mk
-include src/subdir.mk
-include subdir.mk
-include objects.mk
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(C++_DEPS)),)
-include $(C++_DEPS)
endif
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
ifneq ($(strip $(CC_DEPS)),)
-include $(CC_DEPS)
endif
ifneq ($(strip $(CPP_DEPS)),)
-include $(CPP_DEPS)
endif
ifneq ($(strip $(CXX_DEPS)),)
-include $(CXX_DEPS)
endif
ifneq ($(strip $(C_UPPER_DEPS)),)
-include $(C_UPPER_DEPS)
endif
endif
-include ../makefile.defs
# Add inputs and outputs from these tool invocations to the build variables
# All Target
all: Test
# Tool invocations
Test: $(OBJS) $(USER_OBJS)
@echo 'Building target: $@'
@echo 'Invoking: GCC C++ Linker'
g++ -o "Test" $(OBJS) $(USER_OBJS) $(LIBS)
@echo 'Finished building target: $@'
@echo ' '
# Other Targets
clean:
-$(RM) $(OBJS)$(C++_DEPS)$(C_DEPS)$(CC_DEPS)$(CPP_DEPS)$(EXECUTABLES)$(CXX_DEPS)$(C_UPPER_DEPS) Test
-@echo ' '
.PHONY: all clean dependents
.SECONDARY:
-include ../makefile.targets

View File

@ -1,8 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
USER_OBJS :=
LIBS :=

View File

@ -1,27 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
O_SRCS :=
CPP_SRCS :=
C_UPPER_SRCS :=
C_SRCS :=
S_UPPER_SRCS :=
OBJ_SRCS :=
ASM_SRCS :=
CXX_SRCS :=
C++_SRCS :=
CC_SRCS :=
OBJS :=
C++_DEPS :=
C_DEPS :=
CC_DEPS :=
CPP_DEPS :=
EXECUTABLES :=
CXX_DEPS :=
C_UPPER_DEPS :=
# Every subdirectory with source files must be described here
SUBDIRS := \
src \

View File

@ -1 +0,0 @@
src/Test.d: ../src/Test.cpp

Binary file not shown.

View File

@ -1,24 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/Test.cpp
OBJS += \
./src/Test.o
CPP_DEPS += \
./src/Test.d
# Each subdirectory must supply rules for building sources it contributes
src/%.o: ../src/%.cpp
@echo 'Building file: $<'
@echo 'Invoking: GCC C++ Compiler'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

Binary file not shown.

View File

@ -38,19 +38,19 @@ endif
# Add inputs and outputs from these tool invocations to the build variables
# All Target
all: Test
all: CSV
# Tool invocations
Test: $(OBJS) $(USER_OBJS)
CSV: $(OBJS) $(USER_OBJS)
@echo 'Building target: $@'
@echo 'Invoking: GCC C++ Linker'
g++ -o "Test" $(OBJS) $(USER_OBJS) $(LIBS)
g++ -o "CSV" $(OBJS) $(USER_OBJS) $(LIBS)
@echo 'Finished building target: $@'
@echo ' '
# Other Targets
clean:
-$(RM) $(OBJS)$(C++_DEPS)$(C_DEPS)$(CC_DEPS)$(CPP_DEPS)$(EXECUTABLES)$(CXX_DEPS)$(C_UPPER_DEPS) Test
-$(RM) $(OBJS)$(C++_DEPS)$(C_DEPS)$(CC_DEPS)$(CPP_DEPS)$(EXECUTABLES)$(CXX_DEPS)$(C_UPPER_DEPS) CSV
-@echo ' '
.PHONY: all clean dependents

View File

@ -4,5 +4,5 @@
USER_OBJS :=
LIBS := -lopencv_calib3d -lopencv_ts -lopencv_photo -lopencv_videostab -lopencv_video -lopencv_objdetect -lopencv_ml -lopencv_legacy -lopencv_imgproc -lopencv_highgui -lopencv_gpu -lopencv_core -lopencv_contrib -lopencv_flann -lopencv_features2d
LIBS := -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_legacy

View File

@ -1 +0,0 @@
src/Test.d: ../src/Test.cpp

Binary file not shown.

View File

@ -1 +0,0 @@
src/dyn_prog.d: ../src/dyn_prog.c

Binary file not shown.

View File

@ -1 +0,0 @@
src/hsv.d: ../src/hsv.c

Binary file not shown.

View File

@ -1 +0,0 @@
src/my.d: ../src/my.c

Binary file not shown.

View File

@ -4,40 +4,29 @@
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../src/Test.cpp
C_SRCS += \
../src/dyn_prog.c \
../src/hsv.c \
../src/my.c
../src/SvImage.cpp \
../src/SvMain.cpp \
../src/SvProcessorV1.cpp \
../src/SvProcessorV2.cpp
OBJS += \
./src/Test.o \
./src/dyn_prog.o \
./src/hsv.o \
./src/my.o
C_DEPS += \
./src/dyn_prog.d \
./src/hsv.d \
./src/my.d
./src/SvImage.o \
./src/SvMain.o \
./src/SvProcessorV1.o \
./src/SvProcessorV2.o
CPP_DEPS += \
./src/Test.d
./src/SvImage.d \
./src/SvMain.d \
./src/SvProcessorV1.d \
./src/SvProcessorV2.d
# Each subdirectory must supply rules for building sources it contributes
src/%.o: ../src/%.cpp
@echo 'Building file: $<'
@echo 'Invoking: GCC C++ Compiler'
g++ -I/usr/include/c++/4.8 -I/usr/include/opencv -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<"
@echo 'Finished building: $<'
@echo ' '
src/%.o: ../src/%.c
@echo 'Building file: $<'
@echo 'Invoking: GCC C Compiler'
gcc -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<"
g++ -std=c++11 -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

47
src/SvMain.cpp Normal file
View File

@ -0,0 +1,47 @@
//============================================================================
// Name : CSV.cpp
// Author : Andrey
// Version :
// Copyright : Your copyright notice
// Description : Computer Stereo Vision in C++, Ansi-style
//============================================================================
#include <opencv2/opencv.hpp>
#include "SvImage.h"
#include "SvProcessorV1.h"
#include "SvProcessorV2.h"
#include <thread>
using namespace cv;
int main(int argc, char** argv) {
Mat matLeft = imread("img/left12.png", 1);
Mat matRight = imread("img/right12.png", 1);
Mat matStereo(Mat::zeros(matLeft.rows, matLeft.cols, CV_8U));
SvImage left(matLeft);
SvImage right(matRight);
SvImage stereo(matStereo);
int line = 0;
SvProcessorV1 proc[4];
for (int i = 0; i < 4; i++) {
proc[i].setLeftImage(left);
proc[i].setRightImage(right);
}
while (line < stereo.getHeight()) {
for (int i = 0; i < 4; i++) {
if (line < stereo.getHeight() && !proc[i].isActive()) {
proc[i].run(stereo, line++, SvProcessorV1::LEFT);
}
}
}
imshow("Display Image", stereo.getCvMatrix());
waitKey();
return 0;
}

View File

@ -7,6 +7,11 @@
#include "SvProcessorV1.h"
SvProcessorV1::SvProcessorV1() {
left = NULL;
right = NULL;
}
SvProcessorV1::SvProcessorV1(SvImage& left, SvImage& right) {
this->left = &left;
this->right = &right;
@ -21,7 +26,7 @@ int SvProcessorV1::match(int x, int y, int j) {
int error = 0;
for (int i = 0; i <= 2; i++) {
for (int g = 0; g <= 3; g++) {
for (int g = 0; g <= 2; g++) {
error += diff(x + i, y + g, x + i + j, y + g);
}
}
@ -37,7 +42,7 @@ int SvProcessorV1::diff(int lx, int ly, int rx, int ry) {
Bvalue = abs(this->left->getPixel(lx, ly, 2) - this->right->getPixel(rx, ry, 2));
return (Gvalue + Rvalue + Bvalue) +
((abs(Gvalue - Rvalue) + abs(Bvalue - Gvalue)) * 4);
((abs(Gvalue - Rvalue) + abs(Bvalue - Gvalue)) * 2);
}
int SvProcessorV1::match(int x, int y, int j) {
@ -57,57 +62,64 @@ int SvProcessorV1::match(int x, int y, int j) {
error += l > r ? r : l;
error += t > b ? b : t;
//error += l + r+b+t;
//error += (l + r + b + t)/4;
return error;
}
void SvProcessorV1::run(SvImage& stereo, ImageType base = LEFT) {
int SvProcessorV1::calc(SvProcessorV1* proc) {
int x, y, cursor, closest, tmp;
int minErrorValue, tmpSmoothed, matched;
int precursor, preprecursor;
int dist;
for (y = 0; y < stereo.getHeight(); y++) {
cursor = 0;
precursor = 0;
preprecursor = 0;
for (x = 0; x < stereo.getWidth(); x++) {
closest = -1; minErrorValue = -1;matched=0;
minErrorValue=-1;
cursor = 0;
precursor = 0;
preprecursor = 0;
for (int i = 1; i < this->windowSize; i++) {
tmp = this->match(x, y, i);
tmpSmoothed = (tmp) + (abs(cursor - i) * 5);
/*if (tmpSmoothed > 150 ) {
continue;
}*/
for (x = 0; x < proc->m_stereo->getWidth(); x++) {
closest = -1; minErrorValue = -1;matched=0;
minErrorValue=-1;
if (tmpSmoothed < minErrorValue || minErrorValue == -1) {
minErrorValue = tmpSmoothed;
for (int i = 1; i < proc->windowSize; i++) {
dist = (abs(cursor - i));
tmp = match(x, proc->m_line, i);
tmpSmoothed = (tmp) + ((dist * dist * dist) >> 10 );
if (tmpSmoothed < minErrorValue || minErrorValue == -1) {
minErrorValue = tmpSmoothed;
closest = i;
matched = 1;
} else if (tmpSmoothed == minErrorValue) {
if (abs(cursor - i) < abs(cursor - closest)) {
closest = i;
matched = 1;
} else if (tmpSmoothed == minErrorValue) {
if (abs(cursor - i) < abs(cursor - closest)) {
closest = i;
matched++;
}
matched++;
}
}
if (matched == 1) {
cursor = closest;
}
//stereo.putPixel(x, y, minErrorValue/20);
if (cursor != precursor) {
stereo.putPixel(x, y, this->getPixelColor(cursor));
}
preprecursor = precursor;
precursor = cursor;
}
if (matched == 1) {
cursor = closest;
}
//if (cursor != precursor) {
proc->m_stereo->putPixel(x/* + cursor*/, proc->m_line, this->getPixelColor(cursor));
//}
preprecursor = precursor;
precursor = cursor;
}
proc->m_thread.detach();
return 0;
}
int SvProcessorV1::run(SvImage& stereo, int line, ImageType base = LEFT) {
m_stereo = &stereo;
m_line = line;
m_thread = std::thread(&SvProcessorV1::calc, this, this);
return 0;
}
SvProcessorV1::~SvProcessorV1() {

View File

@ -9,6 +9,7 @@
#define SVPROCESSORV1_H_
#include <iostream>
#include <thread>
#include "SvImage.h"
using namespace std;
@ -19,18 +20,31 @@ protected:
SvImage* left;
SvImage* right;
std::thread m_thread;
SvImage* m_stereo;
int m_line;
const int windowSize = 90;
public:
enum ImageType {
LEFT, RIGHT
};
void setLeftImage(SvImage& image) { left = &image;}
void setRightImage(SvImage& image) { right = &image;}
bool isActive() { return m_thread.joinable(); }
int diff(int lx, int ly, int rx, int ry);
int match(int x, int y, int i);
void run(SvImage& stereo, ImageType base);
int run(SvImage& stereo, int line, ImageType base);
int calc(SvProcessorV1* proc);
int getPixelColor(int cursor);
SvProcessorV1(SvImage& left, SvImage& right);
SvProcessorV1();
virtual ~SvProcessorV1();
};

View File

@ -1,31 +0,0 @@
/*
* main.cpp
*
* Created on: 27 июня 2014 г.
* Author: andrey
*/
#include <opencv2/opencv.hpp>
#include "SvImage.h"
#include "SvProcessorV1.h"
#include "SvProcessorV2.h"
using namespace cv;
int main(int argc, char** argv) {
Mat matLeft = imread("img/left11.png", 1);
Mat matRight = imread("img/right11.png", 1);
Mat matStereo(Mat::zeros(matLeft.rows, matLeft.cols, CV_8U));
SvImage left(matLeft);
SvImage right(matRight);
SvImage stereo(matStereo);
SvProcessorV1 proc(left, right);
proc.run(stereo, SvProcessorV1::LEFT);
imshow("Display Image", stereo.getCvMatrix());
waitKey();
return 0;
}