일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- opencv SURF
- 영어명언
- #Android
- 영어
- #영어 명언
- text2img
- #실생활 영어
- word embedding
- #일상영어
- python list
- #opencv
- 완전탐색
- tokenizing
- #1일1영어
- #English
- c언어
- #프로젝트
- python __init__
- convexhull
- Convolution Neural Network
- findContours
- TensorFlow
- python 알고리즘
- tensorflow update
- 이미지 생성
- #영어
- keras
- #실생활영어
- 딥러닝
- object detection
- Today
- Total
When will you grow up?
1_2.ofImage로 이미지를 띄워 이미지 Negative 시키기 본문
1_2.ofImage로 이미지를 띄워 이미지 Negative 시키기
미카이 2016. 9. 8. 15:25저번 1_1 까지는 각 픽셀에 접근을 하여 픽셀의 색상을 입혀 사각형을 띄워 봤습니다.
이번에는 ofImage로 자신의 이미지를 띄워 반전 시키는 코드를 작성해 보겠습니다.
기본적으로
ofImage photo; //원본이미지
ofImage result; //반전시킬 이미지
를 만들어 놓습니다.
그리고 ofApp::setup()
{
photo.load("hello.jpg");
result.clone(photo);
}
이렇게 초기화 문장에서 이미지 파일을 불러와
result이미지에 clone이라는 함수를 이용하여 같은 이미지를 넣어줌니다.
load의 경로는 자신이 만든 프로젝트 안에 bin -> data 파일안에 저장을 넣어주시면 됨니다.
그리고 ofApp::draw()
{
photo.draw(50, 50); //원본이미지를 50,50에 이미지를 그립니다
result.draw(photo.getWidth() + 60, 50); //또한 복제된 이미지를 원본이미지x축으로 // +60 좌표 이미지를 그림니다
}
이미지를 그렸으면 result 이미지를 반전 시킬 함수를 작성 합니다.
void Negative(unsigned char *dst, unsigned char *src, int width, int height) {
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int index = 3 * (y*width + x);
for (int c = 0; c < 3; c++)
{
dst[index + c] = 255 - src[index + c];
}
}
}
}
Negative 함수를 보시면 인자값으로 (반전시킬 이미지 데이터 주소,원본이미지 데이터 주소, 너비, 높이) 로 인자값을 주며
3중 for문을 이용하여 이미지 크기만큼 순회 하며 각 픽셀의 색상을 반전 시켜줍니다.
여기서 핵심은 dst[index + c] = 255 - src[index + c]; 입니다
색상은 0~255까지 표현을 할수 있는데
앞장에서 설명 했듯이 첫번째
r값을 255에서 픽셀을 빼주면서 반복 순회 합니다.
마지막으로 이함수를 불러와 result이미지를 갱신 시켜야겠죠?
void ofApp::update() {
unsigned char *ptr = photo.getPixels().getData();
unsigned char *ptr_result = result.getPixels().getData();
Negative(ptr_result, ptr, photo.getWidth(), photo.getHeight());
result.update();
}
결과 입니다!
왼쪽이미지가 원본이미지 이며,
오른쪽 이미지가 반전시켜진 이미지 입니다
전체 소스를 아래와 같습니다
#include "ofApp.h" const int width = 720; //너비 720 const int height = 480; //높이 480 ofImage result; ofImage photo; //-------------------------------------------------------------- void ofApp::setup() { photo.load("hello.jpg"); result.clone(photo); } void Negative(unsigned char *dst, unsigned char *src, int width, int height) { for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int index = 3 * (y*width + x); for (int c = 0; c < 3; c++) { dst[index + c] = 255 - src[index + c]; } } } } //-------------------------------------------------------------- void ofApp::update() { unsigned char *ptr = photo.getPixels().getData(); unsigned char *ptr_result = result.getPixels().getData(); Negative(ptr_result, ptr, photo.getWidth(), photo.getHeight()); result.update(); }
'02. Study > Computer Vision(openframworks&opencv)' 카테고리의 다른 글
3.blur()를 이용해 이미지 처리후 이미지 키보드 버튼을 이용해 이미지 저장. (2) | 2016.09.10 |
---|---|
2.visualstdio2015에서 opencv+openframeworks 개발환경 구축 (22) | 2016.09.08 |
1_1.setFromPixels()를 이용한 픽셀 색상을 바꿔 사각형 그리기 (0) | 2016.08.31 |
0.openFrameworks 소개 (0) | 2016.08.30 |
1.ofImage 에 대해 알아보자. (0) | 2016.08.30 |