Filip Mitrev
Created February 14, 2021 © Apache-2.0

Land surveying for crop yield optimization

VARI - Poor people's NDVI. We try to recreate the popular NDVI index without the infrared channel, just the RGB! How bad is it?

IntermediateFull instructions providedOver 1 day76

Things used in this project

Hardware components

RDDRONE-FMUK66
NXP RDDRONE-FMUK66
×1
KIT-HGDRONEK66
NXP KIT-HGDRONEK66
×1

Software apps and online services

OpenCV
OpenCV
Microsoft .NET (C#, WPF) for Desktop app and OpenCV wrapper
Ubuntu

Story

Read more

Code

MainWindow.xaml.cs

C#
C# WPF application with GUI using OpenCV wrapper to load and process an ,jpeg.
Manipulates channels and calculates VARI on selected image from file and shows both side by side.
using Emgu.CV.OCR;
using Emgu.CV.UI;
using Emgu.Util;
using Emgu.CV;
using Emgu.CV.Util;
using Emgu.CV.Structure;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing.Imaging;
using System.Drawing;

namespace hovergames
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static Emgu.CV.Image<Rgb, byte> img;
        public static Emgu.CV.Image<Gray, byte> vari;
        public static Bitmap mybmp;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void button_Copy_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog opf = new OpenFileDialog();
            if (opf.ShowDialog() == true)
            {
                Mat myimg=CvInvoke.Imread(opf.FileName,Emgu.CV.CvEnum.LoadImageType.Unchanged);//load IMG from file
                mybmp = myimg.Bitmap;
                img = myimg.ToImage<Rgb,byte>();
                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.UriSource = new Uri(opf.FileName);
                bitmap.EndInit();
                image.Source = bitmap;
            }
        }

        public double scaleBetween(double unscaledNum,double minAllowed,double maxAllowed,double min,double max)
        {
            return (maxAllowed - minAllowed) * (unscaledNum - min) / (max - min) + minAllowed;
        }

        private void button_Copy_Click_1(object sender, RoutedEventArgs e)
        {
            vari = new Image<Gray, byte>(img.Width,img.Height);
            double min = 255;
            double max = 0;
            for (int i=0; i<img.Height;i++)
            {
                for (int j=0;j<img.Width;j++)
                {
                    double delitel = (img[i, j].Green + img[i, j].Red - img[i, j].Blue);
                    double val = (img[i, j].Green - img[i, j].Red) / delitel;
                    if (val > 1)//handle outlayers,extremes
                        val = 1;
                    if (val < 0)
                        val = 0;
                    if (val < min)//keep min and max for scaling afterwards
                        min = val;
                    if (val > max)
                        max = val;
                    vari[i, j] = new Gray(val);//VARI index = (green-red)/(green+red-blue)
                }
            }
            double diff = max - min;
            for (int i = 0; i < img.Height; i++)
            {
                for (int j = 0; j < img.Width; j++)
                {
                    double v = scaleBetween(vari[i, j].Intensity, 0, 255, min, max);
                    vari[i, j] = new Gray(v);
                }
            }
            //bitmap to bitmapSource for GUI show
            var bmp = vari.ToBitmap();
            using (MemoryStream memory = new MemoryStream())
            {
                bmp.Save(memory, ImageFormat.Png);
                memory.Position = 0;
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memory;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
                image_Copy.Source = bitmapImage;
            }
        }
    }
}

getImages.sh

Powershell
A bash script for getting all images titled image(im. number).jpeg from a NavQ running in server mode.
On Navq runs: python -m http.server 8080
and this on the PC to transfer the images.
#!/bin/bash
for i in {1..80}
do
wget http://192.168.1.158:8080/image$i.jpeg
done

Credits

Filip Mitrev

Filip Mitrev

1 project • 0 followers

Comments