Wednesday 19 August 2020

Friday 14 August 2020

How to upgrade the pc RAID1 harddisk from say 4TB to 14TB?

 + upgraded file server RAID1 harddisk (a pair of harddisk) from 4tb to 14tb, steps:-
1. check harddisk type (SATA or SAS) and target size
2. backup the harddisk data into an external harddisk
3. unplug the physical harddisk from motherboard
4. plugin the new physical harddisk into motherboard
5. start the os and configure the RAID1 in bios
6. save and restart the os, format the new disk in windows
7. copy back the data from external harddisk into new internal harddisk
8. reassign the shared folder to each user
9. test the shared folder
10. done

Tuesday 11 August 2020

How to install codeblocks20 in ubuntu16?

(p/s: copied from http://ubuntuhandbook.org/index.php/2020/03/install-codeblocks-20-03-ubuntu-18-04/)

Code::Blocks, open-source C, C++, and Fortran IDE, released version 20.03 a few days ago. Here’s how to install it in Ubuntu 16.04, Ubuntu 18.04, Ubuntu 19.10, and derivatives.

Code::Blocks 20.03 was available for a few days, though it’s not officially announced in its website. The Code::Blocks Developers Team PPA has made the packages for all current Ubuntu releases.

To install the new IDE version, open terminal either by pressing Ctrl+Alt+T on keyboard or by searching for ‘terminal’ from application menu. When it opens, do following steps one by one.

1.) Run command to add the PPA repository:

sudo add-apt-repository ppa:codeblocks-devs/release

Type user password (no asterisk feedback) when it prompts and hit Enter to continue.

2.) If an old version was installed on your system, upgrade it via Software Updater:

or run commands one by one in terminal to install the IDE:

sudo apt update

sudo apt install codeblocks codeblocks-contrib

Once installed, launch it from your system application menu and enjoy!

Uninstall Code::Blocks

To remove the PPA repository, either go to Software & Updates -> Other Software, or run command in terminal:

sudo add-apt-repository --remove ppa:codeblocks-devs/release

And remove Code::Blocks if you want via command:

sudo apt remove --autoremove codeblocks codeblocks-contrib

Monday 10 August 2020

How to draw a circle or rectangle in cpp, example2?

 #include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

// global structure to remember some states changed
// by mouse action and move
struct initRoi
{
    // on off
    int init;

    //initial coordination based on EVENT_LBUTTONDOWN
    int initX;
    int initY;

    // actual coordination
    int actualX;
    int actualY;

    //Selected Rect
    Rect roiRect;

    //Selected Mat roi
    Mat takenRoi;
} SelectedRoi;

//event int is compared to determine action of the mouse EVENT_RBUTTONDOWN
// EVENT_LBUTTONDOWN, EVENT_LBUTTONUP, EVENT_MOUSEMOVE.
// If the event is evauated as happened the global structure SelectedRoi
// is updated.
static void CallBackF(int event, int x, int y, int flags, void* img)
{
//Mouse Right button down
    if (event == EVENT_RBUTTONDOWN)
    {
        cout << "right button " << endl;
        return;
    }
//Mouse Left button down
    if (event == EVENT_LBUTTONDOWN)
    {
        SelectedRoi.initX = x;
        SelectedRoi.initY = y;
        SelectedRoi.init = 1;
        cout << "left button DOWN" << endl;
        return;
    }
//Mouse Left button up
    if (event == EVENT_LBUTTONUP)
    {
        SelectedRoi.actualX = x;
        SelectedRoi.actualX = y;
        cout << "left button UP" << endl;
        return;
    }
//Mouse move coordinates update
    if (event == EVENT_MOUSEMOVE)
    {

        cout << "event mouse move"<< endl;
        SelectedRoi.actualX = x;
        SelectedRoi.actualY = y;
        SelectedRoi.roiRect = Rect(SelectedRoi.initX, SelectedRoi.initY,
                                   SelectedRoi.actualX,  SelectedRoi.actualY);
        return;
    }
}

int main()
{
    // capture
    VideoCapture cap(0);
    // Set initial state for the SelectedRoi structure
//- The init is set = 1 by left button down action
//- This start to display image if (SelectedRoi.init != 0) in main for loop
    SelectedRoi.init = 0;

    for (;;)
    {
        if (!cap.isOpened())
        {
            cout << "Video Capture Fail" << endl;
            break;
        }

        else
        {
            Mat img;
            cap >> img;

            namedWindow("Video", WINDOW_AUTOSIZE);
// mouse call back function, where CallBackF is function
// with parameters event type, x y coorfinates
            setMouseCallback("Video", CallBackF, 0);

            if (SelectedRoi.init != 0)
            {
// draw the rectangle updated by mouse move
                rectangle(img, Rect(SelectedRoi.initX, SelectedRoi.initY,
                                    SelectedRoi.actualX- SelectedRoi.initX, SelectedRoi.actualY- SelectedRoi.initY),
                          Scalar(255, 255, 255), 1, 8, 0);

            }
            imshow("Video", img);
            int key2 = waitKey(20);
        }
    }
}

How to draw a circle or rectangle in cpp?

 #include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

using namespace std;
using namespace cv;

// Function prototypes
void on_mouse(int, int, int, int, void*);


// Public parameters
Mat image(600, 800, CV_8UC3, Scalar(220, 220, 220));
vector<Point> points;
int roi_x0 = 0, roi_y0 = 0, roi_x1 = 0, roi_y1 = 0;
bool start_draw = false;

// Window name for visualisation purposes
const string window_name = "OpenCV Mouse Event Demo";

// FUNCTION : Mouse response for selecting objects in images
// If left button is clicked, start drawing a rectangle and circle as long as mouse moves
//If right button is clicked, look what will happen !
 // Stop drawing once a new left click is detected by the on_mouse function
void on_mouse(int event, int x, int y, int, void *)
{
    // Action when right button is clicked
    if (event == EVENT_RBUTTONDOWN)
    {
        points.push_back(Point(x, y));
        circle(image, Point(x, y), 5, Scalar(255, 255, 255), 1, 8);


        if (points.size() == 3)
        {
            // Find the minimum area enclosing circle
            Point2f center, vtx[4];
            float radius = 0;
            minEnclosingCircle(Mat(points), center, radius);

            circle(image, center, cvRound(radius), Scalar(0, 255, 255), 1);
            points.clear();
        }

        imshow(window_name, image);
    }

    // Action when left button is clicked
    if (event == EVENT_LBUTTONDOWN)
    {
        if (!start_draw)
        {
            roi_x0 = x;
            roi_y0 = y;
            start_draw = true;
        }
        else {
            roi_x1 = x;
            roi_y1 = y;
            start_draw = false;
        }
    }
    // Action when mouse is moving
    if ((event == EVENT_MOUSEMOVE) && start_draw)
    {
        // Redraw bounding box and rectangle
        Mat current_view;
        image.copyTo(current_view);
        rectangle(current_view, Point(roi_x0, roi_y0), Point(x, y), Scalar(0, 0, 255));
        int radius = max(abs(roi_x0 - x), abs(roi_y0 - y));
        circle(current_view, Point(roi_x0, roi_y0), radius, Scalar(255, 0, 0), 1, 8);
        imshow(window_name, current_view);
    }
}


int main(int argc, const char** argv)
{
    // Init window interface and couple mouse actions
    namedWindow(window_name, WINDOW_AUTOSIZE);
    setMouseCallback(window_name, on_mouse);

    imshow(window_name, image);

    int key_pressed = 0;

    do
    {
        // Keys for processing
        // Based on the universal ASCII code of the keystroke: http://www.asciitable.com/
        //      c = 99          add rectangle to current image
        //      <ESC> = 27      exit program
        key_pressed = 0xFF & waitKey(0);
        if (key_pressed==99)
        {
            // draw a rectangle and a circle on the image

            int radius = max(abs(roi_x0 - roi_x1), abs(roi_y0 - roi_y1));
            circle(image, Point(roi_x0, roi_y0), radius, Scalar(0, 0, 255), 1, 8);
            rectangle(image, Point(roi_x0, roi_y0), Point(roi_x1, roi_y1), Scalar(255, 0, 0), 1);
            imshow(window_name, image);
        }
    }
    // Continue as long as the <ESC> key has not been pressed
    while (key_pressed != 27);

    // Close down the window
    destroyWindow(window_name);
    return 0;
}

How to convert opencv Mat to png buffer?

     /* Insert an image. */
    cv::Mat img = cv::imread("logo.png");
    // encode image into jpg
    cv::vector<uchar> buf;
    cv::imencode(".png", img, buf, std::vector<int>() );
    // encoded image is now in buf (a vector)
    unsigned char *imageBuf = new unsigned char [buf.size()];
    memcpy(imageBuf, &buf[0], buf.size());
    //  size of imageBuf is buf.size();
    worksheet_insert_image_buffer(worksheet, 1, 2, imageBuf, buf.size());
    workbook_close(workbook);
    delete[] imageBuf;

Friday 7 August 2020

How to use fopen_s from windows in ubuntu?

 #include <stdio.h>

#ifdef __unix
#define fopen_s(pFile,filename,mode) ((*(pFile))=fopen((filename),(mode)))==NULL
#endif

Wednesday 5 August 2020

How to get time now in python?

import time
now = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(time.time()))