Skip to content

Instantly share code, notes, and snippets.

@PierreZ
Created March 23, 2015 14:24
Show Gist options
  • Save PierreZ/400f62eeae79dc4a4864 to your computer and use it in GitHub Desktop.
Save PierreZ/400f62eeae79dc4a4864 to your computer and use it in GitHub Desktop.
TP OpenCV
// OpenCV séance 1
// Par Pierre Zemb et Alexis Hellouin
#include <opencv2/opencv.hpp>
#include <cmath>
using namespace cv;
void exercice1() {
Mat img = imread("/home/pierrezemb/Downloads/lena.jpg");
imshow("ma fenetre", img);
waitKey();
}
void exercice2() {
//Création d'une image de taille 200*200 avec des unsigned car sur 1 canal
Mat img = Mat::zeros(200,200,CV_8UC1);
int largeur = img.cols;
int hauteur = img.rows;
for (int i = 0; i < largeur/2; i++)
{
for (int j = 0; j < hauteur/2; j++)
{
img.at<unsigned char>(i,j) = 255;
}
}
imshow("ma fenetre", img);
waitKey();
}
void exercice3() {
Mat img = imread("/home/pierrezemb/Downloads/lena.jpg");
Mat imgcropped1 = img(Rect(0,0,img.cols/2,img.rows/2));
Mat imgcropped2 = img(Rect(img.cols/2,img.rows/2,img.cols/4,img.rows/4));
imshow("ma fenetre1", imgcropped1);
imshow("ma fenetre2", imgcropped2);
waitKey();
}
void exercice4() {
vector<Mat> planes;
Mat imgdst;
Mat img = imread("/home/pierrezemb/Downloads/lena.jpg");
cvtColor(img, imgdst, CV_BGR2GRAY);
// Split permet de récupérer les canaux. on y accède avec [0]
split(img,planes);
for (int i = 0; i < 3; ++i)
{
std::cout << planes[i] << std::endl;
}
imshow("ma fenetre1", imgdst);
waitKey();
}
void exercice5() {
Mat img = imread("/home/pierrezemb/Downloads/lena.jpg");
Mat imgdst;
int seuil = 100;
cvtColor(img, imgdst, CV_BGR2GRAY);
for (int i = 0; i < imgdst.rows; i++)
{
for (int j = 0; j < imgdst.cols; j++)
{
if (imgdst.at<unsigned char>(i,j) > seuil)
{
imgdst.at<unsigned char>(i,j) = 255;
}
}
}
imshow("ma fenetre1", imgdst);
waitKey();
}
void exercice6() {
Mat img = imread("/home/pierrezemb/Downloads/lena.jpg");
Mat imgdst;
int thresh = 100;
threshold(img, imgdst, thresh, 255, THRESH_BINARY);
imshow("ma fenetre1", imgdst);
waitKey();
}
void exercice8() {
Mat imgsrc = Mat::zeros(200, 200, CV_8UC1);
// dessine un cercle
// le 1er parametre est l’image sur laquelle dessiner
// le 2nd paramètre est le centre du cercle
// le 3ème paramètre son rayon (int)
// le 4ème paramètre est la couleur (BGR)
// le 5ème paramètre est l’épaisseur de la ligne circle(imgsrc, Point(50,50), 10, Scalar(255,255,255), 3);
// dessine une ligne
line(imgsrc, Point(50,50), Point(100,100), Scalar(200,200,200), 3);
// ecrit un texte
putText(imgsrc, "ISEN", Point(100, 50), FONT_HERSHEY_SIMPLEX, 1, Scalar(100, 100, 100), 3);
// dessine un rectangle
rectangle(imgsrc, Rect(0,0,10,10), Scalar(255,255,255), 3);
imshow("ma fenetre1", imgsrc);
waitKey();
imwrite("monimage.jpg", imgsrc);
}
// void convolCol(Mat imgsrc, Mat& imgdst){
// //imgdst = imgsrc;
// std::cout << imgsrc.rows << " : " << imgsrc.cols << std::endl;
// for (int i = 0; i < imgsrc.rows; i++)
// {
// for (int j = 1; j < imgsrc.cols-1; j++)
// {
// //std::cout << i<< ":" << j << std::endl;
// //std::cout<< "Avant - [" <<i<< ":" << j <<"]=" << imgdst.at<unsigned char>(i,j) << std::endl;
// imgdst.at<unsigned char>(i,j) = (imgsrc.at<unsigned char>(i,j+1) - imgsrc.at<unsigned char>(i,j - 1));
// //std::cout<< "Après - [" <<i<< ":" << j <<"]=" << imgdst.at<unsigned char>(i,j) << std::endl;
// }
// }
// }
void convolCol(Mat imgsrc, Mat& imgdst){
imgdst = imgsrc;
vector<Mat> planes;
split(imgsrc,planes);
// Première boucle pour parcourir chaque vecteur
for (std::vector<Mat>::iterator i = planes.begin(); i != planes.end(); ++i)
{
// (*i) correspond à une image
for (int j = 0; j < (*i).rows; j++)
{
for (int k = 1; k < (*i).cols - 1; k++)
{
(*i).at<unsigned char>(j,k) = fabs(((*i).at<unsigned char>(j,k+1) - (*i).at<unsigned char>(j,k)));
//std::cout<< "après (" << j << ":" << k << ") = " << (*i).at<unsigned char>(j,k) << std::endl;
}
}
}
merge(planes,imgdst);
}
void convolLig(Mat imgsrc, Mat& imgdst){
imgdst = imgsrc;
vector<Mat> planes;
split(imgsrc,planes);
// Première boucle pour parcourir chaque vecteur
for (std::vector<Mat>::iterator i = planes.begin(); i != planes.end(); ++i)
{
// (*i) correspond à une image
for (int j = 1; j < (*i).rows-1; j++)
{
for (int k = 0; k < (*i).cols; k++)
{
(*i).at<unsigned char>(j,k) = fabs(((*i).at<unsigned char>(j+1,k) - (*i).at<unsigned char>(j,k)));
//std::cout<< "après (" << j << ":" << k << ") = " << (*i).at<unsigned char>(j,k) << std::endl;
}
}
}
merge(planes,imgdst);
}
void convolCarre(Mat imgsrc, Mat& imgdst){
imgdst = imgsrc;
vector<Mat> planes;
double carre1,carre2;
//int i = 0;
split(imgsrc,planes);
// Première boucle pour parcourir chaque vecteur de l'image
for (std::vector<Mat>::iterator i = planes.begin(); i != planes.end(); ++i)
{
// (*i) correspond à une image
for (int j = 0; j < (*i).rows; j++)
{
for (int k = 0; k < (*i).cols; k++)
{
carre1 = pow(((*i).at<unsigned char>(j,k+1) - (*i).at<unsigned char>(j,k)),2);
//std::cout << carre1 << std::endl;
carre2 = pow(((*i).at<unsigned char>(j+1,k) - (*i).at<unsigned char>(j,k)),2);
(*i).at<unsigned char>(j,k) = pow(carre1+carre2,0.5);
//std::cout << pow(carre1+carre2,0.5) << std::endl;
}
}
imshow("ma fenetre", (*i));
waitKey();
}
merge(planes,imgdst);
}
void exercice9() {
//Mat imgsrc = imread("/home/pierrezemb/Downloads/lena.jpg");
Mat imgsrc = Mat::zeros(200,200,CV_8UC1);
int largeur = imgsrc.cols;
int hauteur = imgsrc.rows;
for (int i = 0; i < largeur/2; i++)
{
for (int j = 0; j < hauteur/2; j++)
{
imgsrc.at<unsigned char>(i,j) = 255;
}
}
imshow("ma fenetre1", imgsrc);
waitKey();
Mat imgdst;
//convolCol(imgsrc,imgdst);
//convolLig(imgsrc,imgdst);
convolCarre(imgsrc,imgdst);
imshow("ma fenetre1", imgdst);
waitKey();
}
int main(int argc, char *argv[]) {
exercice9();
}
// OpenCV séance 2
// 23 Mars 2015
// Par Pierre Zemb et Alexis Hellouin
#include <opencv2/opencv.hpp>
#include <cmath>
using namespace cv;
// Inversion d'une image
void exercice1() {
Mat lena_gris;
// On charge l'image
Mat lena = imread("/home/pierrezemb/Downloads/lena.jpg");
imshow("lena normal", lena);
waitKey();
cvtColor(lena, lena_gris, CV_BGR2GRAY);
imshow("lena en niveau de gris", lena_gris);
waitKey();
//Création d'une image de taille identique à img avec des unsigned car sur 1 canal
Mat lena_inverse = Mat::zeros(lena.cols,lena.rows,CV_8UC1);
// On boucle sur chaque pixel de l'image
for (int i = 0; i < lena_inverse.rows; i++) {
for (int j = 0; j < lena_inverse.cols; j++) {
lena_inverse.at<unsigned char>(i,j) = 255 - lena_gris.at<unsigned char>(i,j);
}
}
imshow("lena en gris inversé", lena_inverse);
waitKey();
}
void exercice2(){
}
void exercice3() {
Mat lena = imread("/home/pierrezemb/Downloads/lena.jpg");
Mat lena_gris;
cvtColor(lena, lena_gris, CV_BGR2GRAY);
Mat result = Mat::zeros(lena.cols,lena.rows,CV_8UC1);
imshow("image d'origine", lena_gris);
waitKey();
// https://stackoverflow.com/questions/6302171/convert-uchar-mat-to-float-mat-in-opencv
// CV_32FC1 correspond au type float
// Définition de h1
Mat h1(1,3,CV_32FC1);
h1.at<float>(0,0) = -1.0;
h1.at<float>(0,2) = 1.0;
// Définition de h2
Mat h2(3,1,CV_32FC1);
h2.at<float>(0,0) = -1.0;
h2.at<float>(2,0) = 1.0;
// Définition de h3
Mat h3(3,3,CV_32FC1);
h3.at<float>(0,0) = -1;
h3.at<float>(1,0) = -2;
h3.at<float>(2,0) = -1;
h3.at<float>(0,2) = 1;
h3.at<float>(1,2) = 2;
h3.at<float>(2,2) = 1;
// Définition de h4
Mat h4(3,3,CV_32FC1);
h4.at<float>(0,0) = -1;
h4.at<float>(0,1) = -2;
h4.at<float>(0,2) = -1;
h4.at<float>(2,0) = 1;
h4.at<float>(2,1) = 2;
h4.at<float>(2,2) = 1;
// Définition de h5
Mat h5(3,3,CV_32FC1);
h5.at<float>(0,1) = 1;
h5.at<float>(1,0) = 1;
h5.at<float>(1,1) = -4;
h5.at<float>(1,2) = 1;
h5.at<float>(2,1) = 1;
Mat h6 = Mat::ones(3,3,CV_32FC1);
for (int i = 0; i < h6.rows; ++i)
{
for (int j = 0; j < h6.cols; ++j)
{
h6.at<float>(i,j) = h6.at<float>(i,j)/9;
}
}
// Définition de h7
Mat h7(3,3,CV_32FC1);
h7.at<float>(0,1) = -1;
h7.at<float>(1,0) = -1;
h7.at<float>(1,1) = 5;
h7.at<float>(1,2) = -1;
h7.at<float>(2,1) = -1;
// filter2D(InputArray src, OutputArray dst, int ddepth, InputArray kernel, Point anchor=Point(-1,-1), double delta=0, int borderType=BORDER_DEFAULT )
// when ddepth=-1, the output image will have the same depth as the source.
filter2D(lena_gris,result,-1,h1,Point( -1, -1 ), 0, BORDER_DEFAULT );
imwrite("filtre h1.jpg", result);
filter2D(lena_gris,result,-1,h2,Point( -1, -1 ), 0, BORDER_DEFAULT );
imwrite("filtre h2.jpg", result);
filter2D(lena_gris,result,-1,h3,Point( -1, -1 ), 0, BORDER_DEFAULT );
imwrite("filtre h3.jpg", result);
filter2D(lena_gris,result,-1,h4,Point( -1, -1 ), 0, BORDER_DEFAULT );
imwrite("filtre h4.jpg", result);
filter2D(lena_gris,result,-1,h5,Point( -1, -1 ), 0, BORDER_DEFAULT );
imwrite("filtre h5.jpg", result);
filter2D(lena_gris,result,-1,h6,Point( -1, -1 ), 0, BORDER_DEFAULT );
imwrite("filtre h6.jpg", result);
filter2D(lena_gris,result,-1,h7,Point( -1, -1 ), 0, BORDER_DEFAULT );
imwrite("filtre h7.jpg", result);
}
int main(int argc, char *argv[]) {
exercice3();
}
all:
g++ -O2 -Wall -o tp1 main.cpp `pkg-config --cflags --libs opencv`
clean:
rm -rf tp1
@MariaSA2023
Copy link

could you please give me the PDF file of the exercices .?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment