-4

I use this code for print number 0 to 11 in a button:

colorize_combo = gtk_combo_box_text_new_with_entry();   
for (int i = 0; i <= 2; i += 1)
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("%d", i));
gtk_combo_box_set_active(GTK_COMBO_BOX(colorize_combo), 11);
gtk_table_attach_defaults(GTK_TABLE(table), colorize_combo, 0, 1, 17, 18);

I use applyColorMap(unsharp, dstt, type_color) in opencv and I have 12 types color. these types color show up as numbers (0 to 11) . I want to show output as a "text" instead of "number" in my button. I can change types color with "color" button.

I just want to change for example, "0" to "AUTUMN", "1" to "BONE" , .... If you use gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("AUTUMN", 0)); that show up "0" to "AUTUMN" but I want all of them show up just with one gtk_combo_box_text_append_text.

I want to show output as a "text" instead of "number" in my button.

"AUTUMN" instead of "0"

"BONE"   instead of "1"

"JET"    instead of "2"

.

.

.

.

"PARULA" instead of "11"

enter image description here

What ideas on how to solve this task would you suggest? Or on what resource on the internet can I find help?

this is a part of my c++ code:

void incdec2(GtkWidget *widget, const gchar *mode)
{

    else if (!g_strcmp0(mode, "colorized"))
    {

        if (gtk_image_get_pixbuf(GTK_IMAGE(img4)) == NULL)
            return;

        int type_color = atoi(gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(colorize_combo)));

        vector< Vec4i > hierarchy;
        int largest_contour_index = 0;
        int largest_area = 0;

        vector< vector <Point> > contours1;
        Mat alpha(src1.size(), CV_8UC1, Scalar(0));
        normalize(alpha, alpha, 0, 250, NORM_MINMAX, CV_8UC3);
        findContours(thresh, contours1, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image
        for (int i = 0; i< contours1.size(); i++) // iterate through each contour.
        {
            double a = contourArea(contours1[i], false);  //  Find the area of contour
            if (a>largest_area){
                largest_area = a;
                largest_contour_index = i;                //Store the index of largest contour
            }
        }
        drawContours(alpha, contours1, largest_contour_index, Scalar(255), CV_FILLED, 8, hierarchy);

        applyColorMap(unsharp, dstt, type_color);

        split(dstt, rgb);
        Mat rgbe[4] = { rgb[0], rgb[1], rgb[2], alpha };
        merge(rgbe, 4, im_color);

        imwrite("Colorized Image.png", im_color);
        gtk_image_set_from_file(GTK_IMAGE(img4), "Colorized Image.png");

    }

}

int main(int argc, char *argv[])
 {
 .
 .
 .
 .
 .

colorize_combo = gtk_combo_box_text_new_with_entry();   
for (int i = 0; i <= 11; i += 1)
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("%d", i));


 //gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("AUTUMN", 0));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("BONE", 1));
// gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("JET", 2));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("WINTER", 3));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("RAINBOW", 4));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("OCEAN", 5));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("SUMMER", 6));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("SPRING", 7));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("COOL", 8));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("HSV", 9));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("PINK", 10));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("HOT", 11));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("PARULA", 12));

gtk_combo_box_set_active(GTK_COMBO_BOX(colorize_combo), 11);
gtk_table_attach_defaults(GTK_TABLE(table), colorize_combo, 0, 1, 17, 18);


but13 = bold_img_button("Color", "E:/Works for Gov Project/DOC/GUI/logogui1/colorize243.png");
gdk_color_parse("#50a0ff", &color);
gtk_widget_modify_bg(but13, GTK_STATE_NORMAL, &color);
gtk_table_attach_defaults(GTK_TABLE(table), but13, 1, 2, 17, 18);
g_signal_connect(G_OBJECT(but13), "clicked", G_CALLBACK(incdec2), "colorized");


 .
 .
 .
 .

 }

For example, I edit my loop as:

string texts[] = { "AUTUMN", "BONE", "JET" };
int size = sizeof(texts) / sizeof(string);
for (int i = 0; i < size; i++)
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("%s", texts[i]));

but it doesn't work correctly.

Miki
  • 40,887
  • 13
  • 123
  • 202
  • You are specifically asking for the number to show up as numbers with `g_strdup_printf("%d", i)`. Where are the strings coming from? – andlabs Jun 16 '16 at 18:53
  • Why is this tagged as OpenCV? I don't see a trace of it. – Dan Mašek Jun 16 '16 at 19:06
  • @DanMašek the OP seems to just be tagging everything relevant to their overall project; their last few questions were about adding an About OpenCV dialog to their program which I presume uses OpenCV – andlabs Jun 16 '16 at 19:13
  • @DanMašek and andlabs, I edit my question. –  Jun 16 '16 at 19:48
  • 2
    You can create an array of strings and index that in your for loop instead. – andlabs Jun 16 '16 at 20:41
  • @andlabs, please see the OP's comment above. Alireza, you may only ping one user per comment, so andlabs wouldn't get pinged by your last comment. Also, please clarify further where you are getting your values from. A more complete code sample would help. – oldtechaa Jun 16 '16 at 20:41
  • @andlabs, I insert my c++ code. I know that I should create an array of strings and index that in my for loop instead. But I can not write c++ cod for it. and add to my c++ code. –  Jun 16 '16 at 21:03
  • @andlabs, I insert string with c++ (showed in up) but it doesn't work correctly. –  Jun 17 '16 at 06:41

1 Answers1

0

UPDATED with how to decode the value in the combo box

You don't need g_strdup_printf: gtk_combo_box_text_append_text takes a const gchar *. This means it won't modify the string you pass, it will create a copy by itself.

At the top of your file, declare:

static const char *color_names[] = { "AUTUMN", "BONE", "JET" };

Then, where you're filling your combo box, replace:

string texts[] = { "AUTUMN", "BONE", "JET" };
int size = sizeof(texts) / sizeof(string);
for (int i = 0; i < size; i++)
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("%s", texts[i]));

with:

for (int i = 0; i < G_N_ELEMENTS(texts); i++)
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), texts[i]);

Finally, where you read the text selected by the combo box, replace:

int type_color = atoi(gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(colorize_combo)));

with:

int type_color = -1;
gchar * selected_color_name = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(colorize_combo));
for (int i = 0; i < G_N_ELEMENTS(color_names); i++)
    if (g_strcmp0(selected_color_name, color_names[i]) == 0)
        type_color = i;

// gtk_combo_box_text_get_active_text return a string that must be freed as stated in the doc
g_free(selected_color_name);
liberforce
  • 11,189
  • 37
  • 48
  • Thank you so much for guide me. But your code just show up texts and doesn't work correctly. I think because use "!g_strcmp0" in "incdec2" function that show in up. –  Jun 17 '16 at 16:56
  • "doesn't work correctly" isn't the kind of useful feedback one would expect... You wanted to have text instead of numbers, I think that part works well, and that's what your question was about. – liberforce Jun 18 '16 at 10:58
  • BTW, you won't interpret the text in the combo box correctly, if you don't replace the `atoi` call by something else. Remember the combo box doesn't contain a number anymore. – liberforce Jun 18 '16 at 11:07
  • @ liberforce, I'm really sorry for my mention. I like you and thank you so much for guide me. ...... I found problem. in function "applyColorMap(unsharp, dstt, type_color);" type_color is a int numbers and I think we can't show them with texts. Maybe, We can show them as texts with virtual manner instead of numbers in button. –  Jun 18 '16 at 11:40
  • Hey, don't worry, I wasn't pissed off, it's just that on stackoverflow, you ask a single question for a single problem. For your problem, that's what I just said above: `atoi` converts a string to an integer, but has no way to know how to convert a color name like `"AUTUMN"` into an integer. Just move the `texts` array declaration at the top your file (adding the `static` keyword before it), and replace the `atoi` call by a loop that will compare the combo box text with each of the possible values of the `texts` array. Once they match, you know the color index, to put into `type_color`. – liberforce Jun 20 '16 at 11:47
  • @ liberforce, I'm new in this part and If it possible write a sample code for guide me. thank you so much –  Jun 20 '16 at 16:00
  • I updated my answer, please consider approving it if it answers your problem. – liberforce Jun 30 '16 at 10:54