opencv etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
opencv etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

26 Şubat 2021 Cuma

OpenCV Sharr Edge Detection

Giriş
OpenCV Edge Detection için bir sürü yöntem sağlıyor.

Örnek
Açıklaması şöyle
OpenCV has several ways to compute edge detection, and we are going to use Scharr, as it performs quite well. Scharr computes a derivative, so it detects the difference in colors in the image. We are interested in the X axis, and we want the result to be a 64-bit float, so our call would be like this:
Şöyle yaparız
edge_x = cv2.Scharr(channel, cv2.CV_64F, 1, 0)
Açıklaması şöyle
As Scharr computes a derivative, the values can be both positive and negative. We are not interested in the sign, but only in the fact that there is an edge. So, we will take the absolute value: 
Şöyle yaparız
edge_x = np.absolute(edge_x)
Açıklaması şöyle
 Another issue is that the values are not bounded on the 0–255 value range that we expect on a single channel image, and the values are floating points, while we need an 8-bit integer. We can fix both the issues with the following line: 
Şöyle yaparız
edge_x = np.uint8(255 * edge_x / np.max(edge_x))


23 Şubat 2021 Salı

OpenCv cvtColor metodu

Giriş
İmzası şöyle
void cvtColor (InputArray sr, OutputArray dst, int code )
Resmin renklerini değiştirir. OpenCV resimleri BGR formatında yükler.
GRAY ile kaynak resim 1 kanallıdır
BGR ile kaynak resim 3 kanallıdır
BGRA ile kaynak resim 4 kanallıdır

Kaynak resmin renklerini değiştirmeden önce doğru kanal sayısına sahip olduğundan emin olmak gerekir.

Örnek
Resmin renk uzayını değiştirmekteki bir amaç contour 'ları bulmak olabilir. Şöyle yaparız
cv::Mat input = cv::imread("../inputData/RotatedRect.png");

// convert to grayscale (you could load as grayscale instead)
cv::Mat gray;
cv::cvtColor(input,gray, CV_BGR2GRAY);

// compute mask (you could use a simple threshold if the image is always as good as
// the one you provided)
cv::Mat mask; cv::threshold(gray, mask, 0, 255, CV_THRESH_BINARY_INV | CV_THRESH_OTSU); // find contours (if always so easy to segment as your image, you could just
// add the black/rect pixels to a vector)
std::vector<std::vector<cv::Point>> contours; std::vector<cv::Vec4i> hierarchy; cv::findContours(mask,contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
Örnek - 4 kanal
Şöyle yaparız.
Mat srcColor = ...
Mat dstGray;
cvtColor(srcColor, dstGray, CV_BGRA2GRAY);
Örnek
Daha genel bir çözüm için şöyle yaparız.
const cv::Mat img = ...;

//Convert the input image to the input image format
cv::Mat sample;
if (img.channels() == 3 && num_channels_ == 1)
  cv::cvtColor(img, sample, cv::COLOR_BGR2GRAY);
else if (img.channels() == 4 && num_channels_ == 1)
  cv::cvtColor(img, sample, cv::COLOR_BGRA2GRAY);
else if (img.channels() == 4 && num_channels_ == 3)
  cv::cvtColor(img, sample, cv::COLOR_BGRA2BGR);
else if (img.channels() == 1 && num_channels_ == 3)
  cv::cvtColor(img, sample, cv::COLOR_GRAY2BGR);
else
  sample = img;
Örnek
Eğer aynı resmi değiştirmek istersek şöyle yaparız. Yeni OpenCV sürümüğnde CV_ile başlayan sabitler COLOR_ ile başlayan sabitlerle yer değiştirdi.
cvtColor (image, image, COLOR_BGR2GRAY);
Örnek
OpenCV tarafından yüklenmeyen bir resmi siyah beyaz yapmak için şöyle yaparız.
Mat srcColor = ...;
Mat srcGray;
cvtColor(srcColor,srcGray, CV_RGB2GRAY);
Örnek
Diğer renk uzayları arasında dönüşüm yapmak için şöyle yaparız
cvtColor(img, dstimg, cv::COLOR_YUV2BGR);
cvtColor(img, dstimg, cv::COLOR_BGR2RGB);
cvtColor(img, dstimg, CV_BGR2HSV);

10 Eylül 2019 Salı

OpenCV VideoCapture Sınıfı - C++

Giriş
Video yakalamak için kullanılan sınıf VideoCapture. Şu dosya include edilir.
#include "opencv2/opencv.hpp"
Şu dosya da include edilebilir.
#include <opencv/cxcore.hpp>
Java'daki kardeşi için VideoCapture Sınıfı yazısına bakınız.

Kullanım
Yakalanan frame Mat sınıfına yazılır. Mat nesnesi imshow() ile bir pencerede gösterilir.
Örnek
Şöyle yaparız
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main() {
  VideoCapture cam;
  while (!cam.open(0))cerr << "failed to open cam" << endl;
  namedWindow("test");
  while (1) {
    Mat img;
    cam >> img;
    imshow("test", img);
    if (waitKey() == 27)break;
  }
  destroyWindow("test");
}
Örnek
Şöyle yaparız.
Mat frame;
VideoCapture cap ("1.mov");
if (!cap.isOpened ())
  cout << "Error when reading stream";

namedWindow ("Frame", 1);
for (;;)
{
  cap >> frame;
  if (frame.empty())
    break;
  imshow("Frame", frame);
  if (waitKey(10) >= 0)
  {
    break;
  }
}
Default Camera
Örnek
Şöyle yaparız.
VideoCapture capture (0); // open the default camera
Örnek
Eğer ikinci kamerayı açmak istersek şöyle yaparız.
VideoCapture capture (1);
Örnek
İki kamerayı ayrı ayrı göstermek için şöyle yaparız.
namedWindow("OriginalL",0);
namedWindow("OriginalR",2);
VideoCapture captureL(0);
if (!captureL.isOpened()) cout << "L doesn't work" << endl;
VideoCapture captureR(1);
if (!captureR.isOpened()) cout << "R doesn't work" << endl;

for(;;)
{
  captureL >> imageL;
  captureR >> imageR;
  imshow("OriginalL", imageL);
  imshow("OriginalR", imageR);
  if (waitKey(30)== 27)
    break;
}
Constructor - File Path
Şöyle yaparız.
// Open video file
VideoCapture video ("2.avi");
Şöyle yaparız.
cv::VideoCapture capture ("/home/shar/Desktop/op.mp4");
if (!capture.isOpened ())
  return 0;

cv::Mat frame;

bool stop(false);

while(!stop){

  if(!capture.read(frame))
    break;
  
  if(cv::waitKey(10)>=0)
    stop=true;
}
Constructor - gstreamer
Şöyle yaparız. Sanırım OpenCV'nin gstreamer desteği ile derlenmesi gerekiyor.
cv::VideoCapture capture("udpsrc uri=udp://224.3.0.11:5000 ! video/mpegts ! tsdemux
  name=demux ! video/x-h264 ! queue ! decodebin ! glimagesink  demux. ! audio/mpeg
  ! queue ! decodebin ! autoaudiosink");
Constructor - nvdia
Şöyle yaparız. FPS 30'dur
const char* cam = "nvcamerasrc sensor-id=0 ! video/x-raw(memory:NVMM), format=UYVY,
  width=1280, height=720, framerate=30/1 ! nvvidconv flip-method=2 ! video/x-raw,
  format=GRAY8, width=1280, height=720 ! appsink";
VideoCapture capture (cam);
Constructor - Url
Şöyle yaparız
cv::VideoCapture capture("rtsp://root:pass@192.168.0.90/...");
IP kamera için şöyle yaparız.
VideoCapture capture ("http://login:password@111.111.111.111/cgi-bin/snapshot.cgi");
get metodu
Örnek
Frame boyutu için şöyle yaparız.
cv::Size s = cv::Size((int) capture.get(CV_CAP_PROP_FRAME_WIDTH),
                      (int)capture.get(CV_CAP_PROP_FRAME_HEIGHT));
Örnek
ex ve FPS (frames per second) için şöyle yaparız.
int ex  = capture.get (CV_CAP_PROP_FOURCC);
int fps = capture.get (CV_CAP_PROP_FPS);
Örnek
Dosya oynatılırken hangi frame'de olduğumuzu anlamak için şöyle yaparız.
int frameNum = capture.get (CV_CAP_PROP_POS_FRAMES);
Örnek
Toplam frame sayısını bulmak için şöyle yaparız.
int frameCount = capture.get (CV_CAP_PROP_FRAME_COUNT)
grap metodu
"operator >>" işleminin başarılı olup olmadığını döner. Şöyle yaparız.
Mat img;
cap >>img;

if (!capture.grab())
{
  cout << "\n Cannot read the frame from video file" << endl;
  break;
}
isOpened metodu
Şöyle yaparız.
if( !capture.isOpened() )
{
  ...
}
operator >> metodu
Şöyle yaparız.
Mat frame;
capture >> frame; // get a new frame from camera
read metodu - Frame Okuma
VideoCapture sınıfının read() metodu kullanılır
Mat frame;
if(cap.read(frame){...}
release metodu
Şöyle yaparız.
capture.release ();
set metodu
Örnek
Belirtilen frame numarasına gitmek için şöyle yaparız.
int frameNum = ...;
capture.set (CV_CAP_PROP_POS_FRAMES, frameNum);
Sunuç boolean tipidir. Şöyle yaparız.
bool success = capture.set(CV_CAP_PROP_POS_FRAMES, frameNum);
Örnek
Yakalanan frame büyüklüğünü atamak için şöyle yaparız.
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 360);
Örnek
Exposure değerini atamak için şöyle yaparız.
VideoCapture cap(0);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 4008);
cap.set(CV_CAP_PROP_FRAME_WIDTH, 3700);
cap.set(CV_CAP_PROP_EXPOSURE, -2.5);