Skip to content

Instantly share code, notes, and snippets.

View SubhiH's full-sized avatar

Soubhi Hadri SubhiH

View GitHub Profile
@SubhiH
SubhiH / ImageOperator.cpp
Last active July 13, 2023 05:18
Flip image horizontally and vertically
void ImageOperator::flip_h(const unsigned char* input,
const int width,
const int height,
const int channel,
unsigned char*& output){
for (int row = 0; row < height; ++row) {
for (int col = 0; col < width * channel; col += 1) {
output[row*width+col]=input[(height-row)*width+col];
@SubhiH
SubhiH / ImageOperator.cpp
Created December 26, 2018 23:21
Rotate an image
void ImageOperator::rotate(const unsigned char* input,
const int width,
const int height,
const int channel,
unsigned char*& output){
unsigned char* tmp = new unsigned char[width*height*channel];
int step = channel*width;
for (int row = 0; row < height; ++row) {
for (int col = 0; col < width*channel; col+=channel) {
@SubhiH
SubhiH / ImageOperator.cpp
Created December 26, 2018 23:04
Rotate gray scale image
void ImageOperator::rotate(const unsigned char* input,
const int width,
const int height,
const int channel,
unsigned char*& output){
unsigned char* tmp = new unsigned char[width*height*channel];
for (int row = 0; row < height; ++row) {
for (int col = 0; col < width; col+=1) {
tmp[col*width+width-1-row] = input[row*width+col];
int main() {
cv::VideoCapture cam(0);
if (!cam.isOpened()) {
throw std::runtime_error("Error");
}
cv::namedWindow("Window");
while(true){
void ImageOperator::rotate(const unsigned char* input,
const int width,
const int height,
const int channel,
unsigned char* output){
for (int row = 0; row < height; ++row) {
for (int col = 0; col < width; col+=1) {
output[col*width+width-1-row] = input[row*width+col];
}
@SubhiH
SubhiH / main.cpp
Last active December 22, 2018 18:28
Convert RGB image to gray scale test CPP C++
#include <iostream>
#include "opencv2/opencv.hpp"
int main() {
cv::VideoCapture cam(0);
if (!cam.isOpened()) {
throw std::runtime_error("Error");
}
@SubhiH
SubhiH / ImageOperator.cpp
Created December 21, 2018 16:37
Convert RGB image to gray scale by looping through pixels using char raw pointers C++
void ImageOperator::to_gray(const unsigned char* bgr_input,
const int width,
const int height,
const int channel,
unsigned char* gray_output){
int index = 0;
int step = channel*width;
for (int row = 0; row < height; ++row) {
for (int col = 0; col < width*channel; col+=channel) {
gray_output[index] = 0.11*bgr_input[row*step+col]+
@SubhiH
SubhiH / ImageOperator.cpp
Created December 21, 2018 16:08
Convert RGB image to gray scale by looping through pixels (two for loops) using OpenCV
void ImageOperator::to_gray_m3(const cv::Mat &input, cv::Mat &output) {
unsigned char *data_in = (unsigned char*)(input.data);
unsigned char *data_out = (unsigned char*)(output.data);
int index = 0;
for (int row = 0; row < input.rows; ++row) {
for (int col = 0; col < input.cols*input.channels(); col+=input.channels()) {
data_out[index]= 0.11*data_in[row*input.step+col]+
0.59*data_in[row*input.step+col+1]+
@SubhiH
SubhiH / ImageOperator.cpp
Created December 21, 2018 15:29
Convert RGB image to gray scale by looping through pixels using OpenCV
void ImageOperator::to_gray_m2(const cv::Mat &input, cv::Mat &output) {
unsigned char *data_in = (unsigned char*)(input.data);
unsigned char *data_out = (unsigned char*)(output.data);
int index = 0;
int byte_size = input.channels()*input.rows*input.cols;
while(index!=byte_size){
data_out[index/input.channels()] = unsigned(0.11*data_in[index]+0.59*data_in[index+1]+0.3*data_in[index+2]);
index+=3;
@SubhiH
SubhiH / ImageOperator.cpp
Created December 21, 2018 04:48
Convert RGB image to gray scale using iterator in OpenCV C++
void ImageOperator::to_gray_m1(const cv::Mat &input, cv::Mat &output) {
unsigned char *data_out = (unsigned char*)(output.data);
int ind = 0;
auto end = input.end<cv::Vec3b>();
cv::MatConstIterator_<cv::Vec3b> it = input.begin<cv::Vec3b>();
for (; it != end; ++it) {
const unsigned char &r = (*it)[2];
const unsigned char &g = (*it)[1];
const unsigned char &b = (*it)[0];