べた書きするとこんな感じ
カメラの番号が異なる場合はcameraPortを変更します
(タリーランプがついていれば大丈夫。異なっていればusingでexception発生)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;
namespace _01_cameraThrough
{
class Program
{
static void Main(string[] args)
{
ShowCamera();
}
static void ShowCamera()
{
//カメラ映像を表示
int cameraPort = 0;
using (var capture = Cv.CreateCameraCapture(cameraPort))
{
IplImage frame = new IplImage();
// W640 x H480 (default resolution)のウィンドウを作る
int w = 640, h = 480;
// カメラ映像の画サイズ設定
Cv.SetCaptureProperty(capture, CaptureProperty.FrameWidth, w);
Cv.SetCaptureProperty(capture, CaptureProperty.FrameHeight, h);
//ESCキー押さない限り
while (Cv.WaitKey(1) != 27)
{
// 画像の取得
frame = Cv.QueryFrame(capture);
if (frame != null)
{
//frameを表示
Cv.ShowImage("Liveカメラ", frame);
}
}
//ESCキーが押されたら
//全ての画像やwindowを削除
Cv.DestroyAllWindows();
capture.Dispose();
}
}
}
}
参考にさせていただきました
http://www.thedesignium.com/development/8237
2017年10月2日月曜日
表示画像を縮小してみる(Resize)
こんな感じで...
using (var img = new IplImage(@"C:\Users\hogehohe\Pictures\hoge.png")) // 画像を読み込み
{
CvSize size = new CvSize(img.Width / 2, img.Height / 2);
IplImage img2 = new IplImage(size,img.Depth, img.NChannels); // サイズ違いのimgを作成
Cv.Resize(img, img2); // リサイズ
using (new CvWindow(img2)) // ウィンドウを作成して画像を表示
{
Cv.WaitKey();
}
}
using (var img = new IplImage(@"C:\Users\hogehohe\Pictures\hoge.png")) // 画像を読み込み
{
CvSize size = new CvSize(img.Width / 2, img.Height / 2);
IplImage img2 = new IplImage(size,img.Depth, img.NChannels); // サイズ違いのimgを作成
Cv.Resize(img, img2); // リサイズ
using (new CvWindow(img2)) // ウィンドウを作成して画像を表示
{
Cv.WaitKey();
}
}
2017年10月1日日曜日
OpenCvSharpのはじめの一歩
1. Visual Studio 2015などで、File -> プロジェクトの作成
2. ツール -> NuGeパッケージマネージャ -> 参照 -> OpenCvSharp で検索 -> インストール(チェックボックスをチェックしてインストールボタン)
3. 以下のコードを書く
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp; // 追加
namespace consoleApplicationSample
{
class Program
{
static void Main(string[] args)
{
using (var img = new IplImage(@"C:\Users\hogehoge\Pictures\hoge.png")) // 画像を読み込み
{
Cv.Not(img, img); // 画像をネガポジ反転
using (new CvWindow(img)) // ウィンドウを作成して画像を表示
{
Cv.WaitKey();
}
}
}
}
}
2. ツール -> NuGeパッケージマネージャ -> 参照 -> OpenCvSharp で検索 -> インストール(チェックボックスをチェックしてインストールボタン)
3. 以下のコードを書く
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp; // 追加
namespace consoleApplicationSample
{
class Program
{
static void Main(string[] args)
{
using (var img = new IplImage(@"C:\Users\hogehoge\Pictures\hoge.png")) // 画像を読み込み
{
Cv.Not(img, img); // 画像をネガポジ反転
using (new CvWindow(img)) // ウィンドウを作成して画像を表示
{
Cv.WaitKey();
}
}
}
}
}
2017年6月28日水曜日
Navigation Itemのタイトルを表示する
Navigation controllerでタイトルを表示したい場合の例です。
画面遷移後のviewDidLoadで表示を行っています。
var hoge : String? < 前の画面から設定させる場合の箱
...
override func viewDidLoad() {
...
// Navigation controller タイトル表示
if let dispTitle = hoge {
self.title = hoge
}
前の画面でタイトルを指定したい場合などは、前の画面のprepareForSegue(遷移直前に呼ばれるメソッド)で以下のようにします。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "hogehogehoge" {
...
(segue.destinationViewController as? hogehogeViewController)!.hoge = hogedata
画面遷移後のviewDidLoadで表示を行っています。
var hoge : String? < 前の画面から設定させる場合の箱
...
override func viewDidLoad() {
...
// Navigation controller タイトル表示
if let dispTitle = hoge {
self.title = hoge
}
前の画面でタイトルを指定したい場合などは、前の画面のprepareForSegue(遷移直前に呼ばれるメソッド)で以下のようにします。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "hogehogehoge" {
...
(segue.destinationViewController as? hogehogeViewController)!.hoge = hogedata
2017年5月17日水曜日
NSTimerを使って時計表示をしてみる(2)
Timerの生成とRun Loopの登録を別に行う方法です。
NSTimerは毎回作らないといけない模様 (使いまわそうとしても開始しなかった為)
---
import UIKit
class ViewController: UIViewController {
var count:Int = 0
var timer:NSTimer? = nil // タイマー
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
/*
timer = NSTimer(timeInterval: 0.01, // 繰り返し時間(秒)
target: self,
selector: (#selector(ViewController.DispWatchCount)), // ハンドラの指定
userInfo: nil,
repeats: true // 繰り返し
)
NSRunLoop.currentRunLoop().addTimer(timer!, forMode: NSDefaultRunLoopMode)
*/
}
func update() {
print("timer")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet weak var DispTime: UILabel!
@IBAction func stop(sender: AnyObject) {
timer?.invalidate()
}
@IBAction func ButtonStart(sender: AnyObject) {
timer = NSTimer(timeInterval: 0.01, // 繰り返し時間(秒)
target: self,
selector: (#selector(ViewController.DispWatchCount)), // ハンドラの指定
userInfo: nil,
repeats: true // 繰り返し
) // Timerを生成
NSRunLoop.currentRunLoop().addTimer(timer!, forMode: NSDefaultRunLoopMode) // timerを開始 (Run Loopに登録)
}
@IBAction func ButtonStop(sender: AnyObject) {
timer?.invalidate() // timerを停止
}
// Timer handler
func DispWatchCount(){
let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "HH:mm:ss:SS"
let dispTime = formatter.stringFromDate(date)
/* メンバーにアクセスする場合
let calender = NSCalendar.currentCalendar()
let component = calender.components([NSCalendarUnit.Year, NSCalendarUnit.Month, NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Minute, NSCalendarUnit.Second], fromDate: date)
let dispTime = String(component.hour) + ":" + String(component.minute) + ":" + String(component.second)
*/
DispTime.text = dispTime
// count += 1
// DispTime.text = String(count)
}
}
NSTimerは毎回作らないといけない模様 (使いまわそうとしても開始しなかった為)
---
import UIKit
class ViewController: UIViewController {
var count:Int = 0
var timer:NSTimer? = nil // タイマー
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
/*
timer = NSTimer(timeInterval: 0.01, // 繰り返し時間(秒)
target: self,
selector: (#selector(ViewController.DispWatchCount)), // ハンドラの指定
userInfo: nil,
repeats: true // 繰り返し
)
NSRunLoop.currentRunLoop().addTimer(timer!, forMode: NSDefaultRunLoopMode)
*/
}
func update() {
print("timer")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet weak var DispTime: UILabel!
@IBAction func stop(sender: AnyObject) {
timer?.invalidate()
}
@IBAction func ButtonStart(sender: AnyObject) {
timer = NSTimer(timeInterval: 0.01, // 繰り返し時間(秒)
target: self,
selector: (#selector(ViewController.DispWatchCount)), // ハンドラの指定
userInfo: nil,
repeats: true // 繰り返し
) // Timerを生成
NSRunLoop.currentRunLoop().addTimer(timer!, forMode: NSDefaultRunLoopMode) // timerを開始 (Run Loopに登録)
}
@IBAction func ButtonStop(sender: AnyObject) {
timer?.invalidate() // timerを停止
}
// Timer handler
func DispWatchCount(){
let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "HH:mm:ss:SS"
let dispTime = formatter.stringFromDate(date)
/* メンバーにアクセスする場合
let calender = NSCalendar.currentCalendar()
let component = calender.components([NSCalendarUnit.Year, NSCalendarUnit.Month, NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Minute, NSCalendarUnit.Second], fromDate: date)
let dispTime = String(component.hour) + ":" + String(component.minute) + ":" + String(component.second)
*/
DispTime.text = dispTime
// count += 1
// DispTime.text = String(count)
}
}
2017年5月16日火曜日
NSTimerを使って時計表示をしてみる(1)
timerの生成とRunloopへの登録...を同時に行う方法らしいです
1. タイマーを起動する
param3がtimerハンドラを指定
NSTimer.scheduledTimerWithTimeInterval(
0.01, // 繰り返す時間(秒)
target: self,
selector: (#selector(ViewController.DispWatchCount)), // ハンドラ
userInfo: nil,
repeats: true //リピート実行
)
2. timerハンドラの記述
func DispWatchCount(){
...
}
ちなみに等幅フォントとして時計表示部分は Courier 50.0 を使用
---
import UIKit
class ViewController: UIViewController {
var count:Int = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: (#selector(ViewController.DispWatchCount)), userInfo: nil, repeats: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet weak var DispTime: UILabel!
// Timer handler
func DispWatchCount(){
let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "HH:mm:ss:SS"
let dispTime = formatter.stringFromDate(date)
/* メンバーにアクセスする場合
let calender = NSCalendar.currentCalendar()
let component = calender.components([NSCalendarUnit.Year, NSCalendarUnit.Month, NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Minute, NSCalendarUnit.Second], fromDate: date)
let dispTime = String(component.hour) + ":" + String(component.minute) + ":" + String(component.second)
*/
DispTime.text = dispTime
}
}
1. タイマーを起動する
param3がtimerハンドラを指定
NSTimer.scheduledTimerWithTimeInterval(
0.01, // 繰り返す時間(秒)
target: self,
selector: (#selector(ViewController.DispWatchCount)), // ハンドラ
userInfo: nil,
repeats: true //リピート実行
)
2. timerハンドラの記述
func DispWatchCount(){
...
}
ちなみに等幅フォントとして時計表示部分は Courier 50.0 を使用
---
import UIKit
class ViewController: UIViewController {
var count:Int = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: (#selector(ViewController.DispWatchCount)), userInfo: nil, repeats: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet weak var DispTime: UILabel!
// Timer handler
func DispWatchCount(){
let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "HH:mm:ss:SS"
let dispTime = formatter.stringFromDate(date)
/* メンバーにアクセスする場合
let calender = NSCalendar.currentCalendar()
let component = calender.components([NSCalendarUnit.Year, NSCalendarUnit.Month, NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Minute, NSCalendarUnit.Second], fromDate: date)
let dispTime = String(component.hour) + ":" + String(component.minute) + ":" + String(component.second)
*/
DispTime.text = dispTime
}
}
2017年5月15日月曜日
JSONを読む(Array編)
こんな感じのJSONを読みます
{{"midashi":
[{"hoge":"1", "imageurl":"htt://www.hoge.com/uri01.png"},
{"hoge":"2", "imageurl":"htt://www.hoge.com/uri02.png"},
{"hoge":"3", "imageurl":"htt://www.hoge.com/uri03.png"}]
}
// NSUrlSessionの取得ハンドラ
func onFinishGetUri(data: NSData?, res: NSURLResponse?, error: NSError?){
var dict:NSDictionary
do{
dict = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
let imageList = dict["midashi"] as! NSArray
for image in imageList{
let url = image["imageurl"] as! String
print(url)
getList.append(url) // 詰め直してみる
}
// index0のurlを取得
print(getList[0])
} catch {
print("error")
return
}
}
{{"midashi":
[{"hoge":"1", "imageurl":"htt://www.hoge.com/uri01.png"},
{"hoge":"2", "imageurl":"htt://www.hoge.com/uri02.png"},
{"hoge":"3", "imageurl":"htt://www.hoge.com/uri03.png"}]
}
// NSUrlSessionの取得ハンドラ
func onFinishGetUri(data: NSData?, res: NSURLResponse?, error: NSError?){
var dict:NSDictionary
do{
dict = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
let imageList = dict["midashi"] as! NSArray
for image in imageList{
let url = image["imageurl"] as! String
print(url)
getList.append(url) // 詰め直してみる
}
// index0のurlを取得
print(getList[0])
} catch {
print("error")
return
}
}
登録:
投稿 (Atom)