2014年2月27日木曜日
きらきら星
18,18,14,14,13,13,14,14,15,15,16,16,17,17,18,18,14,14,15,15,16,16,17,17,14,14,15,15,16,16,17,17,18,18,14,14,13,13,14,14,15,15,16,16,17,17,18,18
Excelマクロ
Function test(hoge As String)
If hoge = "ど" Then
test = 18
ElseIf hoge = "れ" Then
test = 17
ElseIf hoge = "み" Then
test = 16
ElseIf hoge = "ふぁ" Then
test = 15
ElseIf hoge = "そ" Then
test = 14
ElseIf hoge = "ら" Then
test = 13
ElseIf hoge = "し" Then
test = 12
End If
End Function
If hoge = "ど" Then
test = 18
ElseIf hoge = "れ" Then
test = 17
ElseIf hoge = "み" Then
test = 16
ElseIf hoge = "ふぁ" Then
test = 15
ElseIf hoge = "そ" Then
test = 14
ElseIf hoge = "ら" Then
test = 13
ElseIf hoge = "し" Then
test = 12
End If
End Function
sin波
AudioOutputUnitStop(aU);
-(void)playSound{
//Sampling rate
_sampleRate = 44100.0f;
//Bit rate
bitRate = 8; // 8bit
//AudioComponentDescription
AudioComponentDescription aCD;
aCD.componentType = kAudioUnitType_Output;
aCD.componentSubType = kAudioUnitSubType_RemoteIO;
aCD.componentManufacturer = kAudioUnitManufacturer_Apple;
aCD.componentFlags = 0;
aCD.componentFlagsMask = 0;
//AudioComponent
AudioComponent aC = AudioComponentFindNext(NULL, &aCD);
AudioComponentInstanceNew(aC, &aU);
AudioUnitInitialize(aU);
//コールバック
AURenderCallbackStruct callbackStruct;
callbackStruct.inputProc = renderer;
callbackStruct.inputProcRefCon = (__bridge void*)self;
AudioUnitSetProperty(aU,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input,
0,
&callbackStruct,
sizeof(AURenderCallbackStruct));
//AudioStreamBasicDescription
AudioStreamBasicDescription aSBD;
aSBD.mSampleRate = _sampleRate;
aSBD.mFormatID = kAudioFormatLinearPCM;
aSBD.mFormatFlags = kAudioFormatFlagsAudioUnitCanonical;
aSBD.mChannelsPerFrame = 2;
aSBD.mBytesPerPacket = sizeof(AudioUnitSampleType);
aSBD.mBytesPerFrame = sizeof(AudioUnitSampleType);
aSBD.mFramesPerPacket = 1;
aSBD.mBitsPerChannel = bitRate * sizeof(AudioUnitSampleType);
aSBD.mReserved = 0;
//AudioUnit
AudioUnitSetProperty(aU,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
0,
&aSBD,
sizeof(aSBD));
//再生
AudioOutputUnitStart(aU);
}
static OSStatus renderer(void *inRef,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp* inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData) {
//キャスト
ViewController* def = (__bridge ViewController*)inRef;
//サイン波
float freq = def.frequency*2.0*M_PI/def.sampleRate;
//値を書き込むポインタ
AudioUnitSampleType *outL = ioData->mBuffers[0].mData;
AudioUnitSampleType *outR = ioData->mBuffers[1].mData;
for (int i = 0; i < inNumberFrames; i++) {
// 周波数を計算
float wave = sin(def.phase);
AudioUnitSampleType sample = wave * (1 << kAudioUnitSampleFractionBits);
*outL++ = sample;
*outR++ = sample;
def.phase += freq;
}
return noErr;
};
周波数指定
switch (button.tag) {
case 19: //シ
_frequency = 246.94;
break;
case 18: //ド
_frequency = 261.62;
break;
case 17: //レ
_frequency = 293.66;
break;
case 16: //ミ
_frequency = 329.62;
break;
case 15: //ファ
_frequency = 349.22;
break;
case 14: //ソ
_frequency = 391.99;
break;
case 13: //ラ
_frequency = 440.00;
break;
case 12: //シ
_frequency = 493.88;
break;
case 11: //ド
_frequency = 523.25;
break;
case 10: //レ
_frequency = 587.32;//293.66;
break;
case 9: //ミ
_frequency = 659.25;//329.62;
break;
case 8: //ファ
_frequency = 698.45;//349.22;
break;
case 7: //ソ
_frequency = 783.99;//391.99;
break;
case 6: //ラ
_frequency = 880.00;//440.00;
break;
case 5: //シ
_frequency = 987.76;//493.88;
break;
case 4: //ド
_frequency = 1046.50;//523.25;
break;
case 3: //レ
_frequency = 1174.65;//293.66;
break;
case 2: //ミ
_frequency = 1318.51;//329.62;
break;
case 1: //ファ
_frequency = 1396.91;//349.22;
break;
case 0: //ソ
_frequency = 1567.98;//391.99;
break;
case 20: //ソ♯
_frequency = 1661.21;
break;
case 21: //ファ♯
_frequency = 1479.97;//391.99;
break;
case 23: //レ♯
_frequency =1244.50;//391.99;
break;
case 24: //ド♯
_frequency = 1108.73;//391.99;
break;
case 26: //ラ♯
_frequency = 932.32;//391.99;
break;
case 27: //ソ♯
_frequency = 830.60;//391.99;
break;
case 28: //ファ♯
_frequency = 739.98;//391.99;
break;
case 30: //レ♯
_frequency = 622.25;//391.99;
break;
case 31: //ド♯
_frequency = 554.36;//391.99;
break;
case 33: //ラ♯
_frequency = 466.16;//391.99;
break;
case 34: //ソ♯
_frequency = 415.30;//391.99;
break;
case 35: //ファ♯
_frequency = 369.99;//391.99;
break;
case 37: //レ♯
_frequency = 311.12;//391.99;
break;
case 38: //ド♯
_frequency = 277.18;//391.99;
break;
case 40: //ラ♯
_frequency = 233.08;//391.99;
break;
default:
break;
}
2014年2月16日日曜日
テンプレートマッチング
TemplateMatching.h
--------------------------
#pragma once
#include "cvbase.h"
class TemplateMatching :
public CVBase
{
public:
TemplateMatching(void);
~TemplateMatching(void);
void mainloop(void);
};
TemplateMatching.cpp
--------------------------
#include "TemplateMatching.h"
TemplateMatching::TemplateMatching(void)
{
}
TemplateMatching::~TemplateMatching(void)
{
}
void TemplateMatching::mainloop(void)
{
cv::Mat search_img = cv::imread("room7.png", 1);
if(search_img.empty()) return;
// テンプレート画像
cv::Mat tmp_img = cv::imread("room7_tmp_2.png", 1);
if(tmp_img.empty()) return;
cv::Mat result_img;
// テンプレートマッチング
cv::matchTemplate(search_img, tmp_img, result_img, CV_TM_CCOEFF_NORMED);
// 最大のスコアの場所を探す
cv::Rect roi_rect(0, 0, tmp_img.cols, tmp_img.rows);
cv::Point max_pt;
double maxVal;
cv::minMaxLoc(result_img, NULL, &maxVal, NULL, &max_pt);
roi_rect.x = max_pt.x;
roi_rect.y = max_pt.y;
std::cout << "(" << max_pt.x << ", " << max_pt.y << "), score=" << maxVal << std::endl;
// 探索結果の場所に矩形を描画
cv::rectangle(search_img, roi_rect, cv::Scalar(0,0,255), 3);
cv::namedWindow("search image", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO);
cv::namedWindow("result image", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO);
cv::imshow("search image", search_img);
cv::imshow("result image", result_img);
cv::waitKey(0);
}
--------------------------
#pragma once
#include "cvbase.h"
class TemplateMatching :
public CVBase
{
public:
TemplateMatching(void);
~TemplateMatching(void);
void mainloop(void);
};
TemplateMatching.cpp
--------------------------
#include "TemplateMatching.h"
TemplateMatching::TemplateMatching(void)
{
}
TemplateMatching::~TemplateMatching(void)
{
}
void TemplateMatching::mainloop(void)
{
cv::Mat search_img = cv::imread("room7.png", 1);
if(search_img.empty()) return;
// テンプレート画像
cv::Mat tmp_img = cv::imread("room7_tmp_2.png", 1);
if(tmp_img.empty()) return;
cv::Mat result_img;
// テンプレートマッチング
cv::matchTemplate(search_img, tmp_img, result_img, CV_TM_CCOEFF_NORMED);
// 最大のスコアの場所を探す
cv::Rect roi_rect(0, 0, tmp_img.cols, tmp_img.rows);
cv::Point max_pt;
double maxVal;
cv::minMaxLoc(result_img, NULL, &maxVal, NULL, &max_pt);
roi_rect.x = max_pt.x;
roi_rect.y = max_pt.y;
std::cout << "(" << max_pt.x << ", " << max_pt.y << "), score=" << maxVal << std::endl;
// 探索結果の場所に矩形を描画
cv::rectangle(search_img, roi_rect, cv::Scalar(0,0,255), 3);
cv::namedWindow("search image", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO);
cv::namedWindow("result image", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO);
cv::imshow("search image", search_img);
cv::imshow("result image", result_img);
cv::waitKey(0);
}
指定エリア内のカラー平均グラフの生成
ManyAreaValue.h
--------------------------------
#pragma once
#include "cvbase.h"
#include <vector>
using namespace std;
class Area
{
public:
Area(void);
CvPoint start;
CvPoint end;
int sum[3];
int before[3];
};
class ManyAreaValue :
public CVBase
{
public:
ManyAreaValue(void);
~ManyAreaValue(void);
vector<Area *> areas;
CvPoint pStart;
int speed;
IplImage *graph;
FILE *fp;
cv::VideoCapture cap;
bool mouse;
int count;
int position;
void mouseEvent (int event, int x, int y, int flags, void *param);
void mouseEvent2 (int event, int x, int y, int flags, void *param);
void init(void);
void mainloop(void);
CvScalar createColor(int colorNum);
//void term(void);
void calucurateAreaRGB(IplImage *img);
void printAreaRGB(void);
};
ManyAreaValue.cpp
-----------------------------
#include "ManyAreaValue.h"
void on_mouse (int event, int x, int y, int flags, void *param = NULL);
void on_mouse2 (int event, int x, int y, int flags, void *param = NULL);
Area::Area(void)
{
for (int j=0;j<3;j++){
this->sum[j] = -1;
}
}
ManyAreaValue::ManyAreaValue(void)
{
count = 0;
graph = cvCreateImage( cvSize( 1200, 255*4), IPL_DEPTH_8U, 3);
for (int i=0;i<4;i++){
cvLine (graph, cvPoint(0,255*i), cvPoint(1200,255*i), CV_RGB ( 0, 0, 0), 1, CV_AA, 0);
cvLine (graph, cvPoint(0,255*i+100), cvPoint(1200,255*i+100), CV_RGB ( 100, 100, 100), 1, CV_AA, 0);
cvLine (graph, cvPoint(0,255*i+200), cvPoint(1200,255*i+200), CV_RGB ( 100, 100, 100), 1, CV_AA, 0);
}
}
ManyAreaValue::~ManyAreaValue(void)
{
}
void ManyAreaValue::mouseEvent (int event, int x, int y, int flags, void *param)
{
switch (event) {
case CV_EVENT_MOUSEMOVE:
break;
case CV_EVENT_LBUTTONDOWN:
pStart.x = x;
pStart.y = y;
break;
case CV_EVENT_LBUTTONUP:
Area *area = new Area();
CvPoint pEnd;
area->end.x = x;
area->end.y = y;
area->start = pStart;
areas.push_back(area);
break;
}
}
void ManyAreaValue::mouseEvent2 (int event, int x, int y, int flags, void *param)
{
switch (event) {
case CV_EVENT_MOUSEMOVE:
break;
case CV_EVENT_LBUTTONDOWN:
break;
case CV_EVENT_LBUTTONUP:
int setPos = (int)position/1200;
setPos += x;
count = x;
position = setPos;
cvSetCaptureProperty( this->capture, CV_CAP_PROP_POS_FRAMES, setPos*speed );
//frameNum = x*10;
break;
}
}
void ManyAreaValue::calucurateAreaRGB(IplImage *img)
{
for (int i=0;i<areas.size();i++){
uchar p[3];
for (int j=0;j<3;j++){
areas[i]->before[j] = areas[i]->sum[j] ;
areas[i]->sum[j] = 0;
}
int areaSize = (areas[i]->end.y-areas[i]->start.y) * (areas[i]->end.x-areas[i]->start.x);
for (int y = areas[i]->start.y; y < areas[i]->end.y; y++) {
for (int x = areas[i]->start.x; x < areas[i]->end.x; x++) {
for (int j=0;j<3;j++){
p[0] = img->imageData[img->widthStep * y + x * 3 + 2 - j];
areas[i]->sum[j] += p[0];
}
}
}
for (int j=0;j<3;j++){
areas[i]->sum[j] /= areaSize;
}
}
}
CvScalar ManyAreaValue::createColor(int colorNum)
{
if (colorNum == 0){
return CV_RGB ( 255, 0, 0);
}else if(colorNum == 1){
return CV_RGB ( 0, 255, 0);
}else if (colorNum == 2){
return CV_RGB ( 0, 0, 255);
}
return CV_RGB ( 255, 255, 255);
}
void ManyAreaValue::printAreaRGB(void)
{
double msec = cvGetCaptureProperty(this->capture,CV_CAP_PROP_POS_MSEC);
fprintf(this->fp,"%lf,",msec);
for (int i=0;i<areas.size();i++){
for (int j=0;j<3;j++){
if (areas[i]->before[j] !=-1){
cvLine(graph,
cvPoint(count-1,255*i),
cvPoint(count,255*i+255),
CV_RGB ( 0, 0, 0),
2, CV_AA, 0);
fprintf(this->fp,"%d,",areas[i]->sum[j]);
cvLine (graph,
cvPoint(count-1,255*i+areas[i]->before[j]),
cvPoint(count,255*i+areas[i]->sum[j]),
createColor(j),
2, CV_AA, 0);
}
}
}
fprintf(this->fp,"\n");
cvShowImage ("Graph",this->graph);
}
void ManyAreaValue::init(void)
{
FILE *initfp;
initfp = fopen("ini/ManyAreaValue.txt", "r");
char fileName[256];
int mode;
fscanf(initfp,"%d",&mode);
if (mode == 1){
fscanf(initfp,"%s",&fileName);
fscanf(initfp,"%d",&speed);
this->capture = cvCaptureFromFile(fileName);//"C:\\Users\\aiko\\Documents\\Visual Studio 2010\\Projects\\OpenCVBase\\1_01_H_131129153000.avi");
int areaNum;
fscanf(initfp,"%d",&areaNum);
for (int i=0;i<areaNum;i++){
Area *area = new Area();
fscanf(initfp,"%d",&area->start.x);
fscanf(initfp,"%d",&area->start.y);
fscanf(initfp,"%d",&area->end.x);
fscanf(initfp,"%d",&area->end.y);
this->areas.push_back(area);
}
}else{
this->capture = this->fromCamera();
}
cvNamedWindow( "Capture", 1 );
cvSetMouseCallback ("Capture", on_mouse);
cvNamedWindow( "Graph", 1 );
cvMoveWindow("Graph",0,0);
cvSetMouseCallback ("Capture", on_mouse);
this->fp = fopen("nyan.txt","w");
double fps = cvGetCaptureProperty(this->capture,CV_CAP_PROP_FPS);
}
void ManyAreaValue:: mainloop(void)
{
while (1) {
count++;
position++;
if(count>1200)count = 0;
for (int i=0;i<speed;i++){
this->frame = cvQueryFrame (this->capture);
}
if ( this->frame == NULL ){
cvSetCaptureProperty( this->capture, CV_CAP_PROP_POS_FRAMES, 0.0 );
this->frame = cvQueryFrame(this->capture);
continue;
}
if (this->areas.size() > 0){
this->calucurateAreaRGB(this->frame);
this->printAreaRGB();
for(int i=0;i<this->areas.size();i++)
this->drawLine(this->frame,this->areas[i]->start,this->areas[i]->end,CV_RGB (255, 0, 0),2);
}
cvShowImage ("Capture",this->frame);
ch = cvWaitKey ( 1 ); // 0 はディレイ時間 (ミリ秒単位)
if (ch == '\x1b') {
// ESC キー
break;
}
}
}
--------------------------------
#pragma once
#include "cvbase.h"
#include <vector>
using namespace std;
class Area
{
public:
Area(void);
CvPoint start;
CvPoint end;
int sum[3];
int before[3];
};
class ManyAreaValue :
public CVBase
{
public:
ManyAreaValue(void);
~ManyAreaValue(void);
vector<Area *> areas;
CvPoint pStart;
int speed;
IplImage *graph;
FILE *fp;
cv::VideoCapture cap;
bool mouse;
int count;
int position;
void mouseEvent (int event, int x, int y, int flags, void *param);
void mouseEvent2 (int event, int x, int y, int flags, void *param);
void init(void);
void mainloop(void);
CvScalar createColor(int colorNum);
//void term(void);
void calucurateAreaRGB(IplImage *img);
void printAreaRGB(void);
};
ManyAreaValue.cpp
-----------------------------
#include "ManyAreaValue.h"
void on_mouse (int event, int x, int y, int flags, void *param = NULL);
void on_mouse2 (int event, int x, int y, int flags, void *param = NULL);
Area::Area(void)
{
for (int j=0;j<3;j++){
this->sum[j] = -1;
}
}
ManyAreaValue::ManyAreaValue(void)
{
count = 0;
graph = cvCreateImage( cvSize( 1200, 255*4), IPL_DEPTH_8U, 3);
for (int i=0;i<4;i++){
cvLine (graph, cvPoint(0,255*i), cvPoint(1200,255*i), CV_RGB ( 0, 0, 0), 1, CV_AA, 0);
cvLine (graph, cvPoint(0,255*i+100), cvPoint(1200,255*i+100), CV_RGB ( 100, 100, 100), 1, CV_AA, 0);
cvLine (graph, cvPoint(0,255*i+200), cvPoint(1200,255*i+200), CV_RGB ( 100, 100, 100), 1, CV_AA, 0);
}
}
ManyAreaValue::~ManyAreaValue(void)
{
}
void ManyAreaValue::mouseEvent (int event, int x, int y, int flags, void *param)
{
switch (event) {
case CV_EVENT_MOUSEMOVE:
break;
case CV_EVENT_LBUTTONDOWN:
pStart.x = x;
pStart.y = y;
break;
case CV_EVENT_LBUTTONUP:
Area *area = new Area();
CvPoint pEnd;
area->end.x = x;
area->end.y = y;
area->start = pStart;
areas.push_back(area);
break;
}
}
void ManyAreaValue::mouseEvent2 (int event, int x, int y, int flags, void *param)
{
switch (event) {
case CV_EVENT_MOUSEMOVE:
break;
case CV_EVENT_LBUTTONDOWN:
break;
case CV_EVENT_LBUTTONUP:
int setPos = (int)position/1200;
setPos += x;
count = x;
position = setPos;
cvSetCaptureProperty( this->capture, CV_CAP_PROP_POS_FRAMES, setPos*speed );
//frameNum = x*10;
break;
}
}
void ManyAreaValue::calucurateAreaRGB(IplImage *img)
{
for (int i=0;i<areas.size();i++){
uchar p[3];
for (int j=0;j<3;j++){
areas[i]->before[j] = areas[i]->sum[j] ;
areas[i]->sum[j] = 0;
}
int areaSize = (areas[i]->end.y-areas[i]->start.y) * (areas[i]->end.x-areas[i]->start.x);
for (int y = areas[i]->start.y; y < areas[i]->end.y; y++) {
for (int x = areas[i]->start.x; x < areas[i]->end.x; x++) {
for (int j=0;j<3;j++){
p[0] = img->imageData[img->widthStep * y + x * 3 + 2 - j];
areas[i]->sum[j] += p[0];
}
}
}
for (int j=0;j<3;j++){
areas[i]->sum[j] /= areaSize;
}
}
}
CvScalar ManyAreaValue::createColor(int colorNum)
{
if (colorNum == 0){
return CV_RGB ( 255, 0, 0);
}else if(colorNum == 1){
return CV_RGB ( 0, 255, 0);
}else if (colorNum == 2){
return CV_RGB ( 0, 0, 255);
}
return CV_RGB ( 255, 255, 255);
}
void ManyAreaValue::printAreaRGB(void)
{
double msec = cvGetCaptureProperty(this->capture,CV_CAP_PROP_POS_MSEC);
fprintf(this->fp,"%lf,",msec);
for (int i=0;i<areas.size();i++){
for (int j=0;j<3;j++){
if (areas[i]->before[j] !=-1){
cvLine(graph,
cvPoint(count-1,255*i),
cvPoint(count,255*i+255),
CV_RGB ( 0, 0, 0),
2, CV_AA, 0);
fprintf(this->fp,"%d,",areas[i]->sum[j]);
cvLine (graph,
cvPoint(count-1,255*i+areas[i]->before[j]),
cvPoint(count,255*i+areas[i]->sum[j]),
createColor(j),
2, CV_AA, 0);
}
}
}
fprintf(this->fp,"\n");
cvShowImage ("Graph",this->graph);
}
void ManyAreaValue::init(void)
{
FILE *initfp;
initfp = fopen("ini/ManyAreaValue.txt", "r");
char fileName[256];
int mode;
fscanf(initfp,"%d",&mode);
if (mode == 1){
fscanf(initfp,"%s",&fileName);
fscanf(initfp,"%d",&speed);
this->capture = cvCaptureFromFile(fileName);//"C:\\Users\\aiko\\Documents\\Visual Studio 2010\\Projects\\OpenCVBase\\1_01_H_131129153000.avi");
int areaNum;
fscanf(initfp,"%d",&areaNum);
for (int i=0;i<areaNum;i++){
Area *area = new Area();
fscanf(initfp,"%d",&area->start.x);
fscanf(initfp,"%d",&area->start.y);
fscanf(initfp,"%d",&area->end.x);
fscanf(initfp,"%d",&area->end.y);
this->areas.push_back(area);
}
}else{
this->capture = this->fromCamera();
}
cvNamedWindow( "Capture", 1 );
cvSetMouseCallback ("Capture", on_mouse);
cvNamedWindow( "Graph", 1 );
cvMoveWindow("Graph",0,0);
cvSetMouseCallback ("Capture", on_mouse);
this->fp = fopen("nyan.txt","w");
double fps = cvGetCaptureProperty(this->capture,CV_CAP_PROP_FPS);
}
void ManyAreaValue:: mainloop(void)
{
while (1) {
count++;
position++;
if(count>1200)count = 0;
for (int i=0;i<speed;i++){
this->frame = cvQueryFrame (this->capture);
}
if ( this->frame == NULL ){
cvSetCaptureProperty( this->capture, CV_CAP_PROP_POS_FRAMES, 0.0 );
this->frame = cvQueryFrame(this->capture);
continue;
}
if (this->areas.size() > 0){
this->calucurateAreaRGB(this->frame);
this->printAreaRGB();
for(int i=0;i<this->areas.size();i++)
this->drawLine(this->frame,this->areas[i]->start,this->areas[i]->end,CV_RGB (255, 0, 0),2);
}
cvShowImage ("Capture",this->frame);
ch = cvWaitKey ( 1 ); // 0 はディレイ時間 (ミリ秒単位)
if (ch == '\x1b') {
// ESC キー
break;
}
}
}
登録:
投稿 (Atom)