How to replace from current line in Vim

The common command to search and replace text with confirmation in Vim is :%s/old/new/c. But, this begins the search and replace operation from the beginning of the file. There might be times when you want to begin search and replace from the current line.

The % in :%s/old/new/c is shortcut for 1,$, which means replace from first line to the last line. So, to search and replace from current line to end of file we can use .,$. This can be shortened to ,$. So, the command to search and replace from current line is :,$s/old/new/c

Tried with: Vim 7.4 and Ubuntu 14.04

About these ads

How to set icon for application in Ubuntu

The icon set for an application is displayed when you look for it in the Dash or in the Panel when you are running it. If you find that no icon or a wrong icon is displayed for an application, that can be fixed easily.

There is a .desktop entry corresponding to every application. It will be either in ~/.local/share/applications/ or /usr/share/applications/. This is just a text file. Open it up and find the Icon entry. Set it to a path of the icon file, typically a .xpm file, you want.

Tried with: Ubuntu 14.04

How to get detection score from OpenCV cascade classifier

The OpenCV cascade classifier makes it easy to train and test detection of faces and other objects in images. I have already written about how to train this classifier here. To get the objects detected during the testing stage, OpenCV provides this function:

void CascadeClassifier::detectMultiScale(
    const Mat& image,
    vector<Rect>& objects,
    double scaleFactor=1.1,
    int minNeighbors=3,
    int flags=0,
    Size minSize=Size(),
    Size maxSize=Size()

This returns a list of rectangles in the image where the classifier thinks it has detected the object. However, there seems to be no way to rank the detected locations. There is no detection or confidence weight or score associated with each rectangle to help you prioritize the detected locations. This is surprising since such a score should be present in the classifier, it is just that this function is not exposing that information.

Thankfully, there is an alternative to this function that returns the weight and reject level associated with each detected rectangle. Surprisingly, this function does not yet appear in the documentation. You can find it declared in modules/objdetect/include/opencv2/objdetect/objdetect.hpp as:

CV_WRAP virtual void detectMultiScale(
    const Mat& image,
    CV_OUT vector<Rect>& objects,
    vector<int>& rejectLevels,
    vector<double>& levelWeights,
    double scaleFactor=1.1,
    int minNeighbors=3,
    int flags=0,
    Size minSize=Size(),
    Size maxSize=Size(),
    bool outputRejectLevels=false

You can also hack the code a bit to get other types of scores with each detected location as demonstrated in this post.

Tried with: OpenCV 2.4.9 and Ubuntu 14.04

How to highlight current line in Eclipse

While viewing and editing a wall of text, it is useful to highlight the current line that the cursor lies in. This feature is in Eclipse and can be turned on easily:

  • Go to Window -> Preferences -> General -> Editors -> Text Editors. Enable the option named Highlight current line.

  • If you still do not see current line highlighted, then the highlight color might be set to white or the same as background color of your theme. Go to same place as above, at the bottom you can see Appearance color options. In that list, choose Current line highlight option and set its color to something you like.

Tried with: Eclipse Luna 4.4.1 and Ubuntu 14.04

OpenGL Mathematics (GLM) Library

OpenGL Mathematics (GLM) is a library of mathematics primitives and functions that can be useful in OpenGL, shader, CUDA and other graphics related code. Everyone ends up writing their own version of this code, often with errors and waste of time. GLM could be a good choice in such cases. Note that GLM is not as featured or complex as Eigen.

Briefly, here are the types and operations you can get in GLM:

  • vec3, vec4
  • mat3, mat4
  • Vector operations like normalize, bit swizzling
  • Matrix operations
  • Use of matrices for projection and lighting operations